@canutin/svelte-currency-input 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/utils/parseAbbrValue.js +10 -1
- package/package.json +1 -1
|
@@ -17,7 +17,16 @@ export const parseAbbrValue = (value, decimalSeparator = '.') => {
|
|
|
17
17
|
if (match) {
|
|
18
18
|
const [, digits, , abbr] = match;
|
|
19
19
|
const multiplier = abbrMap[abbr.toLowerCase()];
|
|
20
|
-
|
|
20
|
+
const result = Number(digits.replace(decimalSeparator, '.')) * multiplier;
|
|
21
|
+
// Round based on input precision to avoid floating-point errors
|
|
22
|
+
// e.g. 4.1 * 1000000 = 4099999.9999999995 should become 4100000
|
|
23
|
+
// but 1.12345678 * 1000 = 1123.45678 should preserve all decimals
|
|
24
|
+
const decimalPart = digits.split(decimalSeparator)[1] || '';
|
|
25
|
+
const inputDecimals = decimalPart.length;
|
|
26
|
+
const multiplierZeros = Math.log10(multiplier);
|
|
27
|
+
const resultDecimals = Math.max(0, inputDecimals - multiplierZeros);
|
|
28
|
+
const precision = Math.pow(10, resultDecimals);
|
|
29
|
+
return Math.round(result * precision) / precision;
|
|
21
30
|
}
|
|
22
31
|
return undefined;
|
|
23
32
|
};
|