@@ -3,7 +3,39 @@ import { getFormatter } from '../../utils/formatNumber';
33
44const STEP_EPSILON_FACTOR = 1e-10 ;
55
6- function getFractionDigits ( format ?: Intl . NumberFormatOptions ) {
6+ // The repo's configured Intl types do not include the newer NumberFormat v3 rounding options yet.
7+ type NumberFormatOptionsWithRounding = Intl . NumberFormatOptions & {
8+ roundingIncrement ?:
9+ | 1
10+ | 2
11+ | 5
12+ | 10
13+ | 20
14+ | 25
15+ | 50
16+ | 100
17+ | 200
18+ | 250
19+ | 500
20+ | 1000
21+ | 2000
22+ | 2500
23+ | 5000
24+ | undefined ;
25+ roundingMode ?:
26+ | 'ceil'
27+ | 'floor'
28+ | 'expand'
29+ | 'trunc'
30+ | 'halfCeil'
31+ | 'halfFloor'
32+ | 'halfExpand'
33+ | 'halfTrunc'
34+ | 'halfEven'
35+ | undefined ;
36+ } ;
37+
38+ function getFractionDigits ( format ?: NumberFormatOptionsWithRounding ) {
739 const defaultOptions = getFormatter ( 'en-US' ) . resolvedOptions ( ) ;
840 const minimumFractionDigits =
941 format ?. minimumFractionDigits ?? defaultOptions . minimumFractionDigits ?? 0 ;
@@ -14,17 +46,34 @@ function getFractionDigits(format?: Intl.NumberFormatOptions) {
1446 return { maximumFractionDigits, minimumFractionDigits } ;
1547}
1648
17- function roundToFractionDigits ( value : number , maximumFractionDigits : number ) {
49+ export function roundToFractionDigits (
50+ value : number ,
51+ maximumFractionDigits : number ,
52+ format ?: NumberFormatOptionsWithRounding ,
53+ ) {
1854 if ( ! Number . isFinite ( value ) ) {
1955 return value ;
2056 }
2157 const digits = Math . min ( Math . max ( maximumFractionDigits , 0 ) , 20 ) ;
22- return Number ( value . toFixed ( digits ) ) ;
58+
59+ if ( format ?. roundingIncrement == null && format ?. roundingMode == null ) {
60+ return Number ( value . toFixed ( digits ) ) ;
61+ }
62+
63+ const roundingFormatOptions : NumberFormatOptionsWithRounding = {
64+ useGrouping : false ,
65+ minimumFractionDigits : digits ,
66+ maximumFractionDigits : digits ,
67+ roundingIncrement : format ?. roundingIncrement ,
68+ roundingMode : format ?. roundingMode ,
69+ } ;
70+
71+ return Number ( getFormatter ( 'en-US' , roundingFormatOptions ) . format ( value ) ) ;
2372}
2473
25- export function removeFloatingPointErrors ( value : number , format ?: Intl . NumberFormatOptions ) {
74+ export function removeFloatingPointErrors ( value : number , format ?: NumberFormatOptionsWithRounding ) {
2675 const { maximumFractionDigits } = getFractionDigits ( format ) ;
27- return roundToFractionDigits ( value , maximumFractionDigits ) ;
76+ return roundToFractionDigits ( value , maximumFractionDigits , format ) ;
2877}
2978
3079function snapToStep (
@@ -73,7 +122,7 @@ export function toValidatedNumber(
73122 minWithDefault : number ;
74123 maxWithDefault : number ;
75124 minWithZeroDefault : number ;
76- format : Intl . NumberFormatOptions | undefined ;
125+ format : NumberFormatOptionsWithRounding | undefined ;
77126 snapOnStep : boolean ;
78127 small : boolean ;
79128 clamp : boolean ;
0 commit comments