rounding.js 741 B

123456789101112131415161718192021222324252627
  1. /**
  2. * Created by Tony on 2017/3/28.
  3. */
  4. var test = require('tape');
  5. test('test javascript rounding: ', function (t) {
  6. var 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. test('test javascript fixed rounding: ', function (t) {
  14. var f1 = 2.35, f2 = 2.45, f3 = 2.449995;
  15. t.equal(f1.toFixed(1), '2.4');
  16. t.equal(f2.toFixed(1), '2.5');
  17. t.equal(f3.toFixed(1), '2.5');
  18. t.end();
  19. });
  20. //test('test javascript decimal: ', function (t) {
  21. // var x = new Decimal(0.1), y = new Decimal(0.2);
  22. // t.equal(x + y, 0.3);
  23. // t.end();
  24. //});