关于到固定
返回包含此 Number 值的字符串,该数值以十进制定点表示法表示,小数点后有 fractionDigits 位。如果 fractionDigits 未定义,则假定为 0。具体来说,执行以下步骤:
算法Number.prototype.toFixed (fractionDigits)
:https ://www.ecma-international.org/ecma-262/5.1/#sec-15.7.4.5
toFixed 方法的长度属性为 1。
- 如果使用多个参数调用 toFixed 方法,则行为未定义(参见第 15 节)。
对于小于 0 或大于 20 的 fractionDigits 的值,允许实现扩展 toFixed 的行为。在这种情况下,toFixed 不一定会为这些值抛出 RangeError。
注意toFixed 的输出对于某些值可能比 toString 更精确,因为 toString 只打印足够的有效数字来区分数字和相邻的数字值。
JS 变通
function fix(n, p) {
return (+(Math.round(+(n + 'e' + p)) + 'e' + -p)).toFixed(p);
}
let exampleA = fix(49.1175, 3);
let exampleB = fix(49.1775, 3);
let exampleC = fix(49.775, 2);
const random = Math.random();
console.log(exampleA);
console.log(exampleB);
console.log(exampleC);
console.log('Before:', random, 'After Custom =>', fix(random, 3), 'Default:', random.toFixed(3));
// 49.118
// 49.178
// 49.78
需要精度
我建议只是简单地set precision
从C++移植到 Node.JS 模块。
child_process
您可以简单地在Node.JS中安装并使用 a来调用带有参数的C++程序,并让C++运行一个函数来将值转换并输出到控制台。