0

I need to refactor lodash _.round() to use decimal.js toDecimalPlaces()

Which of the decimal.js rounding methods matches the way lodash rounds?

enter image description here

steampowered
  • 11,809
  • 12
  • 78
  • 98

1 Answers1

0

It seems rounding mode 7 which is ROUND_HALF_CEIL matches lodash _.round()

Decimal.set({rounding: Decimal.ROUND_HALF_CEIL});
console.log(Decimal.rounding,'Decimal.rounding');
// 7 'Decimal.rounding'
var test1 = Decimal(3.15).toDP(1).toString();
console.log(test1,'test1');
// 3.2 test1
var test2 = Decimal(-3.15).toDP(1).toString();
console.log(test2,'test2');
// -3.1 test2
var test3 = _.round(3.15, 1);
console.log(test3,'test3');
// 3.2 test3
var test4 = _.round(-3.15, 1);
console.log(test4,'test4');
// -3.1 test4

NOTE: this is not an exhaustive test. I remember from experience modes 1, 2, 5, 6, 8, and 9 are not correct.

steampowered
  • 11,809
  • 12
  • 78
  • 98