简体   繁体   English

什么 decimal.js 舍入模式匹配 lodash 舍入?

[英]What decimal.js rounding mode matches lodash rounding?

I need to refactor lodash _.round() to use decimal.js toDecimalPlaces()我需要重构lodash _.round()以使用decimal.js toDecimalPlaces()

Which of the decimal.js rounding methods matches the way lodash rounds?哪种decimal.js 舍入方法与 lodash 舍入方式匹配?

在此处输入图像描述

It seems rounding mode 7 which is ROUND_HALF_CEIL matches lodash _.round()似乎 ROUND_HALF_CEIL 的舍入模式 7 与ROUND_HALF_CEIL _.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.我记得从经验模式 1、2、5、6、8 和 9 是不正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM