-
Notifications
You must be signed in to change notification settings - Fork 12
chore: move to Intl.NumberFormat for formatting numbers #120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
76666d2
f31ef11
c80f811
97707e4
498af9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -126,11 +126,12 @@ describe("CoreChart: axes", () => { | |||||||
| test("uses default numeric axes formatters for integer values", () => { | ||||||||
| renderChart({ highcharts, options: { series, xAxis: { title: { text: "X" } }, yAxis: { title: { text: "Y" } } } }); | ||||||||
| getAxisOptionsFormatters().forEach((formatter) => { | ||||||||
| // See https://www.unicode.org/cldr/cldr-aux/charts/29/verify/numbers/en.html | ||||||||
|
||||||||
| expect(formatter.call(mockAxisContext({ value: 0 }))).toBe("0"); | ||||||||
| expect(formatter.call(mockAxisContext({ value: 1 }))).toBe("1"); | ||||||||
| expect(formatter.call(mockAxisContext({ value: 1_000 }))).toBe("1K"); | ||||||||
| expect(formatter.call(mockAxisContext({ value: 1_000_000 }))).toBe("1M"); | ||||||||
| expect(formatter.call(mockAxisContext({ value: 1_000_000_000 }))).toBe("1G"); | ||||||||
| expect(formatter.call(mockAxisContext({ value: 1_000_000_000 }))).toBe("1B"); | ||||||||
| }); | ||||||||
| }); | ||||||||
|
|
||||||||
|
|
@@ -140,7 +141,8 @@ describe("CoreChart: axes", () => { | |||||||
| expect(formatter.call(mockAxisContext({ value: 2.0 }))).toBe("2"); | ||||||||
| expect(formatter.call(mockAxisContext({ value: 2.03 }))).toBe("2.03"); | ||||||||
| expect(formatter.call(mockAxisContext({ value: 0.03 }))).toBe("0.03"); | ||||||||
| expect(formatter.call(mockAxisContext({ value: 0.003 }))).toBe("3e-3"); | ||||||||
| expect(formatter.call(mockAxisContext({ value: 0.003 }))).toBe("0.003"); | ||||||||
| expect(formatter.call(mockAxisContext({ value: 0.0000000003 }))).toBe("3E-10"); | ||||||||
|
||||||||
| Type | Example pattern | Meaning |
|---|---|---|
| E | # E 0 |
This marks a scientific format. The E symbol may change position. Must be retained. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -102,30 +102,25 @@ function secondFormatter(value: number) { | |
| }); | ||
| } | ||
|
|
||
| function numberFormatter(value: number): string { | ||
| const format = (num: number) => parseFloat(num.toFixed(2)).toString(); // trims unnecessary decimals | ||
|
|
||
| function numberFormatter(value: number, locale: string = "en-US"): string { | ||
|
||
| const absValue = Math.abs(value); | ||
|
|
||
| if (absValue === 0) { | ||
| return "0"; | ||
| } | ||
|
|
||
| if (absValue < 0.01) { | ||
| return value.toExponential(0); | ||
| } | ||
|
|
||
| if (absValue >= 1e9) { | ||
| return format(value / 1e9) + "G"; | ||
| } | ||
|
|
||
| if (absValue >= 1e6) { | ||
| return format(value / 1e6) + "M"; | ||
| } | ||
|
|
||
| if (absValue >= 1e3) { | ||
| return format(value / 1e3) + "K"; | ||
| // Use scientific notation for very small numbers | ||
| if (absValue < 1e-9) { | ||
| return new Intl.NumberFormat(locale, { | ||
| notation: "scientific", | ||
| maximumFractionDigits: 9, | ||
| }).format(value); | ||
| } | ||
|
|
||
| return format(value); | ||
| // Use compact notation for normal range | ||
| return new Intl.NumberFormat(locale, { | ||
| notation: "compact", | ||
| compactDisplay: "short", | ||
| maximumFractionDigits: 9, | ||
| }).format(value); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ | |
| "target": "ES2019", | ||
| "jsx": "react-jsx", | ||
| "types": [], | ||
| "lib": ["es2019", "dom", "dom.iterable"], | ||
| "lib": ["es2020", "dom", "dom.iterable"], | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this change needed? |
||
| "module": "ESNext", | ||
| "moduleResolution": "Node", | ||
| "esModuleInterop": true, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a specific reason why the threshold of this condition changes from 0.01 (i.e, 1e-2) to 1e-9?
@pan-kot Would this be a problem?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am open to the discussion about that. It may be valuable to expose that setting to the chart props?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can do that in the test pages - but let's not change the current precision in the default chart formatters. What is good for one team might not work for another.
All formatters can be already overridden in both public and core APIs.