@@ -47,15 +47,21 @@ describe('formatNumber', () => {
4747 expect ( formatNumber ( ) ) . toEqual ( "" ) ;
4848 } ) ;
4949
50- it ( 'should return empty string if null value is passed' , ( ) => {
50+ it ( 'should return empty string if null or undefined value is passed' , ( ) => {
5151 expect ( formatNumber ( null ) ) . toEqual ( "" ) ;
52+ expect ( formatNumber ( undefined ) ) . toEqual ( "" ) ;
5253 } ) ;
5354
5455 it ( 'should return value if not finite value is passed' , ( ) => {
5556 expect ( formatNumber ( Infinity ) ) . toBe ( Infinity ) ;
5657 expect ( formatNumber ( "foo" ) ) . toEqual ( "foo" ) ;
5758 } ) ;
5859
60+ it ( 'should limit precision' , ( ) => {
61+ const value = 5.4654647884512e+96 ;
62+ expect ( formatNumber ( value , '#.#' ) ) . toEqual ( value . toFixed ( 20 ) ) ;
63+ } ) ;
64+
5965
6066 describe ( 'errors' , ( ) => {
6167 currencyData . supplemental . currencyData . region . CUSTOM = [ { XXX : { } } ] ;
@@ -82,24 +88,26 @@ describe('formatNumber', () => {
8288} ) ;
8389
8490describe ( 'standard scientific formatting' , ( ) => {
91+ const value = 123 ;
92+
8593 it ( 'should apply format' , ( ) => {
86- const number = 123 ;
87- expect ( formatNumber ( number , 'e' ) ) . toEqual ( number . toExponential ( ) ) ;
94+ expect ( formatNumber ( value , 'e' ) ) . toEqual ( value . toExponential ( ) ) ;
8895 } ) ;
8996
9097 it ( 'should apply format with precision' , ( ) => {
91- const number = 123 ;
92- expect ( formatNumber ( number , 'e10' ) ) . toEqual ( number . toExponential ( 10 ) ) ;
98+ expect ( formatNumber ( value , 'e10' ) ) . toEqual ( value . toExponential ( 10 ) ) ;
9399 } ) ;
94100
95101 it ( 'should apply format when passing options object' , ( ) => {
96- const number = 123 ;
97- expect ( formatNumber ( number , { style : 'scientific' } ) ) . toEqual ( number . toExponential ( ) ) ;
102+ expect ( formatNumber ( value , { style : 'scientific' } ) ) . toEqual ( value . toExponential ( ) ) ;
98103 } ) ;
99104
100105 it ( 'should apply format with precision when passing options object' , ( ) => {
101- const number = 123 ;
102- expect ( formatNumber ( number , { style : 'scientific' , minimumFractionDigits : 10 } ) ) . toEqual ( number . toExponential ( 10 ) ) ;
106+ expect ( formatNumber ( value , { style : 'scientific' , minimumFractionDigits : 10 } ) ) . toEqual ( value . toExponential ( 10 ) ) ;
107+ } ) ;
108+
109+ it ( 'should use locale specific decimal separator' , ( ) => {
110+ expect ( formatNumber ( value , { style : 'scientific' } , 'bg' ) ) . toEqual ( '1,23e+2' ) ;
103111 } ) ;
104112} ) ;
105113
@@ -226,6 +234,14 @@ describe('standard decimal formatting', () => {
226234 expect ( formatNumber ( 33111110 , "n" , "custom" ) ) . toEqual ( "33111110" ) ;
227235 } ) ;
228236
237+ //doesn't seem to be a locale with zero group size so not sure if this is needed
238+ it ( 'should not add group if the integer length is equal to the non-zero group sizes' , ( ) => {
239+ loadCustom ( { pattern : ",,###,##0.###" } ) ;
240+ console . log ( JSON . stringify ( cldr . custom , null , 4 ) ) ;
241+
242+ expect ( formatNumber ( 123456 , "n" , "custom" ) ) . toEqual ( "123,456" ) ;
243+ } ) ;
244+
229245} ) ;
230246
231247describe ( 'standard percent formatting' , ( ) => {
@@ -336,6 +352,22 @@ describe('custom formatting', () => {
336352 expect ( formatNumber ( - 18000 , '#,##0' ) ) . toEqual ( "-18,000" ) ;
337353 } ) ;
338354
355+ it ( 'formats currency' , ( ) => {
356+ expect ( formatNumber ( 10 , '$#.#' ) ) . toEqual ( "$10" ) ;
357+ } ) ;
358+
359+ it ( 'formats currency with locale symbol' , ( ) => {
360+ expect ( formatNumber ( 10 , '#.#$' , 'bg' ) ) . toEqual ( "10лв." ) ;
361+ } ) ;
362+
363+ it ( 'formats percentage' , ( ) => {
364+ expect ( formatNumber ( 0.5 , '#.#%' ) ) . toEqual ( "50%" ) ;
365+ } ) ;
366+
367+ it ( 'percentage does not leave trailing zeros if multiplication by 100 causes rounding error' , ( ) => {
368+ expect ( formatNumber ( 0.035 , '#.##%' ) ) . toEqual ( "3.5%" ) ;
369+ } ) ;
370+
339371 it ( 'applies thousand separator to a longer than the pattern number' , ( ) => {
340372 expect ( formatNumber ( 1000000.1 , '#,###' ) ) . toEqual ( "1,000,000" ) ;
341373 } ) ;
@@ -404,6 +436,11 @@ describe('custom formatting', () => {
404436 expect ( formatNumber ( 10 , "# \\%" ) ) . toEqual ( "10 %" ) ;
405437 } ) ;
406438
439+ it ( "formats with question mark as literal" , ( ) => {
440+ expect ( formatNumber ( 10 , "?\\$#" ) ) . toEqual ( "?$10" ) ;
441+ expect ( formatNumber ( 10 , "\\?\\$#" ) ) . toEqual ( "?$10" ) ;
442+ } ) ;
443+
407444 it ( "formats with quote as literal" , ( ) => {
408445 expect ( formatNumber ( 10 , "# \"%\"" ) ) . toEqual ( "10 %" ) ;
409446 } ) ;
@@ -442,6 +479,13 @@ describe('custom formatting', () => {
442479 expect ( formatNumber ( 3.235555 , "0.#0" ) ) . toEqual ( "3.24" ) ;
443480 } ) ;
444481
482+ it ( "removes trailing zeros after rounding" , ( ) => {
483+ expect ( formatNumber ( 0.016999999999 , "#.#####" ) ) . toEqual ( "0.017" ) ;
484+ expect ( formatNumber ( 0.016999999999 , "#.0000#" ) ) . toEqual ( "0.0170" ) ;
485+ expect ( formatNumber ( 1.999 , "0.0#" ) ) . toEqual ( "2.0" ) ;
486+ expect ( formatNumber ( 1.999 , "0.#" ) ) . toEqual ( "2" ) ;
487+ } ) ;
488+
445489 it ( "removes decimal part if no number placeholder" , ( ) => {
446490 expect ( formatNumber ( 3.222 , "0." ) ) . toEqual ( "3" ) ;
447491 } ) ;
@@ -455,15 +499,19 @@ describe('custom formatting', () => {
455499 } ) ;
456500
457501 it ( "applies negative format rounding" , ( ) => {
458- expect ( formatNumber ( - 0.001 , "####;-(#.#)" ) ) . toEqual ( "-(0.0)" ) ;
502+ expect ( formatNumber ( - 0.001 , "####;-(#.#)" ) ) . toEqual ( "-(0)" ) ;
503+ } ) ;
504+
505+ it ( "toString decimal number -1000 with negative format" , ( ) => {
506+ expect ( formatNumber ( - 1000 , "#,##0;(#,##0);-" ) ) . toEqual ( "(1,000)" ) ;
459507 } ) ;
460508
461509 it ( "applies negative format" , ( ) => {
462510 expect ( formatNumber ( - 123 , "####;-(#.00)" ) ) . toEqual ( "-(123.00)" ) ;
463511 } ) ;
464512
465513 it ( "clears negative sign if rounded number is positive" , ( ) => {
466- expect ( formatNumber ( - 0.00001 , "#.##" ) ) . toEqual ( "0.00 " ) ;
514+ expect ( formatNumber ( - 0.00001 , "#.##" ) ) . toEqual ( "0" ) ;
467515 } ) ;
468516
469517 it ( "formats 0" , ( ) => {
0 commit comments