toFormat.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. if (typeof Test === 'undefined') require('../tester');
  2. Test('toFormat', function () {
  3. function t(expected, value, dp){
  4. Test.areEqual(expected, new BigNumber(value).toFormat(dp));
  5. }
  6. var format = {
  7. decimalSeparator: '.',
  8. groupSeparator: ',',
  9. groupSize: 3,
  10. secondaryGroupSize: 0,
  11. fractionGroupSeparator: ' ',
  12. fractionGroupSize: 0
  13. };
  14. BigNumber.config({
  15. DECIMAL_PLACES: 20,
  16. ROUNDING_MODE: 4,
  17. RANGE: 1E9,
  18. EXPONENTIAL_AT: [-7, 21],
  19. FORMAT: format
  20. });
  21. t('0', 0);
  22. t('1', 1);
  23. t('-1', -1);
  24. t('123.456', 123.456);
  25. t('NaN', NaN);
  26. t('Infinity', 1/0);
  27. t('-Infinity', -1/0);
  28. t('0', 0, null);
  29. t('1', 1, undefined);
  30. t('-1', -1, 0);
  31. t('123.456', 123.456, 3);
  32. t('NaN', NaN, 0);
  33. t('Infinity', 1/0, 3);
  34. t('-Infinity', -1/0, 0);
  35. t('0.0', 0, 1);
  36. t('1.00', 1, 2);
  37. t('-1.000', -1, 3);
  38. t('123.4560', 123.456, 4);
  39. t('NaN', NaN, 5);
  40. t('Infinity', 1/0, 6);
  41. t('-Infinity', -1/0, 7);
  42. t('9,876.54321', 9876.54321);
  43. t('4,018,736,400,000,000,000,000', '4.0187364e+21');
  44. t('999,999,999,999,999', 999999999999999);
  45. t('99,999,999,999,999', 99999999999999);
  46. t('9,999,999,999,999', 9999999999999);
  47. t('999,999,999,999', 999999999999);
  48. t('99,999,999,999', 99999999999);
  49. t('9,999,999,999', 9999999999);
  50. t('999,999,999', 999999999);
  51. t('99,999,999', 99999999);
  52. t('9,999,999', 9999999);
  53. t('999,999', 999999);
  54. t('99,999', 99999);
  55. t('9,999', 9999);
  56. t('999', 999);
  57. t('99', 99);
  58. t('9', 9);
  59. t('76,852.342091', '7.6852342091e+4');
  60. format.groupSeparator = ' ';
  61. t('76 852.34', '7.6852342091e+4', 2);
  62. t('76 852.342091', '7.6852342091e+4');
  63. t('76 852.3420910871', '7.6852342091087145832640897e+4', 10);
  64. format.fractionGroupSize = 5;
  65. t('4 018 736 400 000 000 000 000', '4.0187364e+21');
  66. t('76 852.34209 10871 45832 64089', '7.685234209108714583264089e+4', 20);
  67. t('76 852.34209 10871 45832 64089 7', '7.6852342091087145832640897e+4', 21);
  68. t('76 852.34209 10871 45832 64089 70000', '7.6852342091087145832640897e+4', 25);
  69. t('999 999 999 999 999', 999999999999999, 0);
  70. t('99 999 999 999 999.0', 99999999999999, 1);
  71. t('9 999 999 999 999.00', 9999999999999, 2);
  72. t('999 999 999 999.000', 999999999999, 3);
  73. t('99 999 999 999.0000', 99999999999, 4);
  74. t('9 999 999 999.00000', 9999999999, 5);
  75. t('999 999 999.00000 0', 999999999, 6);
  76. t('99 999 999.00000 00', 99999999, 7);
  77. t('9 999 999.00000 000', 9999999, 8);
  78. t('999 999.00000 0000', 999999, 9);
  79. t('99 999.00000 00000', 99999, 10);
  80. t('9 999.00000 00000 0', 9999, 11);
  81. t('999.00000 00000 00', 999, 12);
  82. t('99.00000 00000 000', 99, 13);
  83. t('9.00000 00000 0000', 9, 14);
  84. t('1.00000 00000 00000', 1, 15);
  85. t('1.00000 00000 0000', 1, 14);
  86. t('1.00000 00000 000', 1, 13);
  87. t('1.00000 00000 00', 1, 12);
  88. t('1.00000 00000 0', 1, 11);
  89. t('1.00000 00000', 1, 10);
  90. t('1.00000 0000', 1, 9);
  91. format.fractionGroupSize = 0;
  92. t('4 018 736 400 000 000 000 000', '4.0187364e+21');
  93. t('76 852.34209108714583264089', '7.685234209108714583264089e+4', 20);
  94. t('76 852.342091087145832640897', '7.6852342091087145832640897e+4', 21);
  95. t('76 852.3420910871458326408970000', '7.6852342091087145832640897e+4', 25);
  96. t('999 999 999 999 999', 999999999999999, 0);
  97. t('99 999 999 999 999.0', 99999999999999, 1);
  98. t('9 999 999 999 999.00', 9999999999999, 2);
  99. t('999 999 999 999.000', 999999999999, 3);
  100. t('99 999 999 999.0000', 99999999999, 4);
  101. t('9 999 999 999.00000', 9999999999, 5);
  102. t('999 999 999.000000', 999999999, 6);
  103. t('99 999 999.0000000', 99999999, 7);
  104. t('9 999 999.00000000', 9999999, 8);
  105. t('999 999.000000000', 999999, 9);
  106. t('99 999.0000000000', 99999, 10);
  107. t('9 999.00000000000', 9999, 11);
  108. t('999.000000000000', 999, 12);
  109. t('99.0000000000000', 99, 13);
  110. t('9.00000000000000', 9, 14);
  111. t('1.000000000000000', 1, 15);
  112. t('1.00000000000000', 1, 14);
  113. t('1.0000000000000', 1, 13);
  114. t('1.000000000000', 1, 12);
  115. t('1.00000000000', 1, 11);
  116. t('1.0000000000', 1, 10);
  117. t('1.000000000', 1, 9);
  118. format = {
  119. decimalSeparator: '.',
  120. groupSeparator: ',',
  121. groupSize: 3,
  122. secondaryGroupSize: 2
  123. };
  124. BigNumber.config({ FORMAT: format });
  125. t('9,876.54321', 9876.54321);
  126. t('10,00,037.123', '1000037.123456789', 3);
  127. t('4,01,87,36,40,00,00,00,00,00,000', '4.0187364e+21');
  128. t('99,99,99,99,99,99,999', 999999999999999);
  129. t('9,99,99,99,99,99,999', 99999999999999);
  130. t('99,99,99,99,99,999', 9999999999999);
  131. t('9,99,99,99,99,999', 999999999999);
  132. t('99,99,99,99,999', 99999999999);
  133. t('9,99,99,99,999', 9999999999);
  134. t('99,99,99,999', 999999999);
  135. t('9,99,99,999', 99999999);
  136. t('99,99,999', 9999999);
  137. t('9,99,999', 999999);
  138. t('99,999', 99999);
  139. t('9,999', 9999);
  140. t('999', 999);
  141. t('99', 99);
  142. t('9', 9);
  143. format.decimalSeparator = ',';
  144. format.groupSeparator = '.';
  145. t('1.23.45.60.000,000000000008', '1.23456000000000000000789e+9', 12);
  146. format.groupSeparator = '';
  147. t('10000000000123456789000000,0000000001', '10000000000123456789000000.000000000100000001', 10);
  148. format.groupSeparator = ' ';
  149. format.groupSize = 1;
  150. format.secondaryGroupSize = 4;
  151. t('4658 0734 6509 8347 6580 3645 0,6', '4658073465098347658036450.59764985763489569875659876459', 1);
  152. format.fractionGroupSize = 2;
  153. format.fractionGroupSeparator = ':';
  154. format.secondaryGroupSize = null;
  155. t('4 6 5 8 0 7 3 4 6 5 0 9 8 3 4 7 6 5 8 0 3 6 4 5 0,59:76:49:85:76:34:89:56:98:75:65:98:76:45:9', '4658073465098347658036450.59764985763489569875659876459' );
  156. });