rounding.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. /**
  2. * Created by Tony on 2017/3/28.
  3. */
  4. let test = require('tape');
  5. // test('test javascript rounding: ', function (t) {
  6. // let f1 = 2.35, f2 = 2.45, f3 = 2.449999999999999995, f4 = 2.44999999999999995;
  7. // t.equal(Math.round(f1*10), 24);
  8. // t.equal(Math.round(f2*10), 25);
  9. // t.equal(Math.round(f3*10), 25);
  10. // t.equal(Math.round(f4*10), 25);
  11. // t.end();
  12. // });
  13. //
  14. // test('test javascript fixed rounding: ', function (t) {
  15. // let f1 = 2.35, f2 = 2.45, f3 = 2.449995;
  16. // t.equal(f1.toFixed(1), '2.4');
  17. // t.equal(f2.toFixed(1), '2.5');
  18. // t.equal(f3.toFixed(1), '2.5');
  19. // t.end();
  20. // });
  21. //test('test javascript decimal: ', function (t) {
  22. // let x = new Decimal(0.1), y = new Decimal(0.2);
  23. // t.equal(x + y, 0.3);
  24. // t.end();
  25. //});
  26. test('test javascript decimal: ', function (t) {
  27. let x = '3.44999999999';
  28. t.equal(parseFloat(x).toFixed(1), `3.5`);
  29. t.equal(parseFloat(x).toFixed(6), `3.450000`);
  30. t.equal(parseFloat(parseFloat(x).toFixed(6)).toFixed(1), `3.5`);
  31. t.end();
  32. });