toNumber.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. if (typeof Test === 'undefined') require('../tester');
  2. Test('toNumber', function () {
  3. function isMinusZero(n) {
  4. return n === 0 ? 1 / n === -Infinity : null
  5. }
  6. function t(value, n) {
  7. Test.areEqual(n, new BigNumber(value).toNumber());
  8. }
  9. BigNumber.config({
  10. DECIMAL_PLACES: 20,
  11. ROUNDING_MODE: 4,
  12. RANGE: 1E9,
  13. EXPONENTIAL_AT: 1E9
  14. });
  15. Test.areEqual(false, isMinusZero(new BigNumber('0').toNumber()));
  16. Test.areEqual(false, isMinusZero(new BigNumber('0.0').toNumber()));
  17. Test.areEqual(false, isMinusZero(new BigNumber('0.000000000000').toNumber()));
  18. Test.areEqual(false, isMinusZero(new BigNumber('0e+0').toNumber()));
  19. Test.areEqual(false, isMinusZero(new BigNumber('0e-0').toNumber()));
  20. Test.areEqual(false, isMinusZero(new BigNumber('1e-1000000000').toNumber()));
  21. Test.areEqual(true, isMinusZero(new BigNumber('-0').toNumber()));
  22. Test.areEqual(true, isMinusZero(new BigNumber('-0.0').toNumber()));
  23. Test.areEqual(true, isMinusZero(new BigNumber('-0.000000000000').toNumber()));
  24. Test.areEqual(true, isMinusZero(new BigNumber('-0e+0').toNumber()));
  25. Test.areEqual(true, isMinusZero(new BigNumber('-0e-0').toNumber()));
  26. Test.areEqual(true, isMinusZero(new BigNumber('-1e-1000000000').toNumber()));
  27. t(1, 1);
  28. t('1', 1);
  29. t('1.0', 1);
  30. t('1e+0', 1);
  31. t('1e-0', 1);
  32. t(12345.6789, 12345.6789);
  33. t(-1, -1);
  34. t('-1', -1);
  35. t('-1.0', -1);
  36. t('-1e+0', -1);
  37. t('-1e-0', -1);
  38. t(Infinity, 1 / 0);
  39. t('Infinity', 1 / 0);
  40. t(-Infinity, -1 / 0);
  41. t('-Infinity', -1 / 0);
  42. t(NaN, NaN);
  43. t('NaN', NaN);
  44. t('9.999999e+1000000000', 1 / 0);
  45. t('-9.999999e+1000000000', -1 / 0);
  46. t('1e-1000000000', 0);
  47. t('-1e-1000000000', -0);
  48. });