diff --git a/hdyc-svelte/src/lib/utils/formatConversionValue.js b/hdyc-svelte/src/lib/utils/formatConversionValue.js new file mode 100644 index 0000000..d2ab1f2 --- /dev/null +++ b/hdyc-svelte/src/lib/utils/formatConversionValue.js @@ -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 }; diff --git a/hdyc-svelte/src/lib/utils/formatConversionValue.ts b/hdyc-svelte/src/lib/utils/formatConversionValue.ts index fb4adbd..2296a8d 100644 --- a/hdyc-svelte/src/lib/utils/formatConversionValue.ts +++ b/hdyc-svelte/src/lib/utils/formatConversionValue.ts @@ -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'); }