mirror of
https://github.com/FoggedLens/iD.git
synced 2026-02-12 16:52:50 +00:00
Avoid truncation when formatting numbers
This commit is contained in:
@@ -429,7 +429,7 @@ export function coreLocalizer() {
|
||||
'formatToParts' in Intl.NumberFormat.prototype)) {
|
||||
return (number) => number.toString();
|
||||
} else {
|
||||
return (number) => number.toLocaleString(locale);
|
||||
return (number) => number.toLocaleString(locale, { maximumFractionDigits: 20 });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -437,7 +437,7 @@ export function coreLocalizer() {
|
||||
// https://stackoverflow.com/a/55366435/4585461
|
||||
const polyfill = (string) => parseFloat(string.trim());
|
||||
if (!('Intl' in window && 'NumberFormat' in Intl)) return polyfill;
|
||||
const format = new Intl.NumberFormat(locale);
|
||||
const format = new Intl.NumberFormat(locale, { maximumFractionDigits: 20 });
|
||||
if (!('formatToParts' in format)) return polyfill;
|
||||
const parts = format.formatToParts(12345.6);
|
||||
const numerals = Array.from({ length: 10 }).map((_, i) => format.format(i));
|
||||
|
||||
@@ -15,6 +15,7 @@ describe('iD.coreLocalizer', function() {
|
||||
expect(parseFloat(formatFloat(1.234))).to.eql(1.234);
|
||||
expect(parseFloat(formatFloat(1234))).to.eql(1234);
|
||||
expect(parseFloat(formatFloat(1234.56))).to.eql(1234.56);
|
||||
expect(parseFloat(formatFloat(3.14159))).to.eql(3.14159);
|
||||
});
|
||||
it('roundtrips Spanish numbers', function () {
|
||||
var localizer = iD.coreLocalizer();
|
||||
@@ -24,6 +25,7 @@ describe('iD.coreLocalizer', function() {
|
||||
expect(parseFloat(formatFloat(1.234))).to.eql(1.234);
|
||||
expect(parseFloat(formatFloat(1234))).to.eql(1234);
|
||||
expect(parseFloat(formatFloat(1234.56))).to.eql(1234.56);
|
||||
expect(parseFloat(formatFloat(3.14159))).to.eql(3.14159);
|
||||
});
|
||||
it('roundtrips Arabic numbers', function () {
|
||||
var localizer = iD.coreLocalizer();
|
||||
@@ -33,6 +35,7 @@ describe('iD.coreLocalizer', function() {
|
||||
expect(parseFloat(formatFloat(1.234))).to.eql(1.234);
|
||||
expect(parseFloat(formatFloat(1234))).to.eql(1234);
|
||||
expect(parseFloat(formatFloat(1234.56))).to.eql(1234.56);
|
||||
expect(parseFloat(formatFloat(3.14159))).to.eql(3.14159);
|
||||
});
|
||||
it('roundtrips Bengali numbers', function () {
|
||||
var localizer = iD.coreLocalizer();
|
||||
@@ -42,6 +45,7 @@ describe('iD.coreLocalizer', function() {
|
||||
expect(parseFloat(formatFloat(1.234))).to.eql(1.234);
|
||||
expect(parseFloat(formatFloat(1234))).to.eql(1234);
|
||||
expect(parseFloat(formatFloat(1234.56))).to.eql(1234.56);
|
||||
expect(parseFloat(formatFloat(3.14159))).to.eql(3.14159);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user