rounding.js 551 B

1234567891011121314151617181920
  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.end();
  11. });
  12. test('test javascript fixed rounding: ', function (t) {
  13. var f1 = 2.35, f2 = 2.45, f3 = 2.449995;
  14. t.equal(f1.toFixed(1), '2.4');
  15. t.equal(f2.toFixed(1), '2.5');
  16. t.equal(f3.toFixed(1), '2.5');
  17. t.end();
  18. });