File tree Expand file tree Collapse file tree 2 files changed +8
-1
lines changed Expand file tree Collapse file tree 2 files changed +8
-1
lines changed Original file line number Diff line number Diff line change 1+ const MAX_PRECISION = 20 ;
2+
13export default function round ( value , precision ) {
24 let result = value ;
35 let decimals = precision || 0 ;
@@ -8,5 +10,5 @@ export default function round(value, precision) {
810 result = result . toString ( ) . split ( 'e' ) ;
911 result = Number ( result [ 0 ] + 'e' + ( result [ 1 ] ? ( Number ( result [ 1 ] ) - decimals ) : - decimals ) ) ;
1012
11- return result . toFixed ( decimals ) ;
13+ return result . toFixed ( Math . min ( decimals , MAX_PRECISION ) ) ;
1214}
Original file line number Diff line number Diff line change @@ -56,6 +56,11 @@ describe('formatNumber', () => {
5656 expect ( formatNumber ( "foo" ) ) . toEqual ( "foo" ) ;
5757 } ) ;
5858
59+ it ( 'should limit precision' , ( ) => {
60+ const value = 5.4654647884512e+96 ;
61+ expect ( formatNumber ( value , '#.#' ) ) . toEqual ( value . toFixed ( 20 ) ) ;
62+ } ) ;
63+
5964
6065 describe ( 'errors' , ( ) => {
6166 currencyData . supplemental . currencyData . region . CUSTOM = [ { XXX : { } } ] ;
You can’t perform that action at this time.
0 commit comments