feat: enhance value formatting by adding scientific notation for extremely small numbers.

This commit is contained in:
Ben
2026-03-10 14:48:00 -07:00
parent ae95a66668
commit d23e910aa7
2 changed files with 31 additions and 1 deletions

View File

@@ -0,0 +1,24 @@
function formatConversionValue(value: number | null | undefined): string {
if (value === null || value === undefined || Number.isNaN(value)) {
return '—';
}
if (!Number.isFinite(value)) {
return value.toString();
}
if (value === 0) {
return '0';
}
const rounded = parseFloat(value.toFixed(6));
if (rounded !== 0) {
return rounded.toString();
}
const precise = value.toFixed(12).replace(/\.?0+$/, '');
if (precise !== '0' && precise !== '') {
return precise;
}
// Fallback for extremely small values: use scientific notation but clean it up
const scientific = value.toExponential();
return scientific.replace(/\.?0+e/, 'e');
}
module.exports = { formatConversionValue };

View File

@@ -13,5 +13,11 @@ export function formatConversionValue(value: number | null | undefined): string
return rounded.toString();
}
const precise = value.toFixed(12).replace(/\.?0+$/, '');
return precise || '0';
if (precise !== '0' && precise !== '') {
return precise;
}
// Fallback for extremely small values: use scientific notation but clean it up
const scientific = value.toExponential();
return scientific.replace(/\.?0+e/, 'e');
}