Skip to content

Commit b072a73

Browse files
fix(formatNumber): use locale specific decimal separator for scientific formatting
1 parent 82562a6 commit b072a73

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/numbers/standard-number-format.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const DECIMAL_PLACEHOLDER = "n";
1212
const CURRENCY = "currency";
1313
const PERCENT = "percent";
1414
const EMPTY = "";
15+
const POINT = ".";
1516

1617
function fractionOptions(options) {
1718
let { minimumFractionDigits, maximumFractionDigits, style } = options;
@@ -69,14 +70,15 @@ function currencyUnitPattern(info, value) {
6970

7071

7172
export default function standardNumberFormat(number, options, info) {
73+
const symbols = info.numbers.symbols;
7274
const { style } = options;
7375

7476
//return number in exponential format
7577
if (style === "scientific") {
76-
return options.minimumFractionDigits !== undefined ? number.toExponential(options.minimumFractionDigits) : number.toExponential();
78+
let exponential = options.minimumFractionDigits !== undefined ? number.toExponential(options.minimumFractionDigits) : number.toExponential();
79+
return exponential.replace(POINT, symbols.decimal);
7780
}
7881

79-
const symbols = info.numbers.symbols;
8082
let value = number;
8183
let symbol;
8284

test/numbers.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,24 +87,26 @@ describe('formatNumber', () => {
8787
});
8888

8989
describe('standard scientific formatting', () => {
90+
const value = 123;
91+
9092
it('should apply format', () => {
91-
const number = 123;
92-
expect(formatNumber(number, 'e')).toEqual(number.toExponential());
93+
expect(formatNumber(value, 'e')).toEqual(value.toExponential());
9394
});
9495

9596
it('should apply format with precision', () => {
96-
const number = 123;
97-
expect(formatNumber(number, 'e10')).toEqual(number.toExponential(10));
97+
expect(formatNumber(value, 'e10')).toEqual(value.toExponential(10));
9898
});
9999

100100
it('should apply format when passing options object', () => {
101-
const number = 123;
102-
expect(formatNumber(number, { style: 'scientific' })).toEqual(number.toExponential());
101+
expect(formatNumber(value, { style: 'scientific' })).toEqual(value.toExponential());
103102
});
104103

105104
it('should apply format with precision when passing options object', () => {
106-
const number = 123;
107-
expect(formatNumber(number, { style: 'scientific', minimumFractionDigits: 10})).toEqual(number.toExponential(10));
105+
expect(formatNumber(value, { style: 'scientific', minimumFractionDigits: 10})).toEqual(value.toExponential(10));
106+
});
107+
108+
it('should use locale specific decimal separator', () => {
109+
expect(formatNumber(value, { style: 'scientific' }, 'bg')).toEqual('1,23e+2');
108110
});
109111
});
110112

0 commit comments

Comments
 (0)