feat: enhance value formatting by adding scientific notation for extremely small numbers.
This commit is contained in:
24
hdyc-svelte/src/lib/utils/formatConversionValue.js
Normal file
24
hdyc-svelte/src/lib/utils/formatConversionValue.js
Normal 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 };
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user