Wal*_*ker 344 javascript rounding
你可以将javascript中的数字舍入到小数点后的1个字符(正确舍入)吗?
我尝试了*10,round,/ 10但它在int的末尾留下了两位小数.
Bil*_*oon 665
Math.round( num * 10) / 10
工作,这是一个例子......
var number = 12.3456789;
var rounded = Math.round( number * 10 ) / 10;
// rounded is 12.3
Run Code Online (Sandbox Code Playgroud)
如果你想让它有一个小数位,即使它是0,然后添加...
var fixed = rounded.toFixed(1);
// fixed is always to 1dp
// BUT: returns string!
// to get it back to number format
parseFloat( number.toFixed(2) )
// 12.34
// but that will not retain any trailing zeros
// so, just make sure it is the last step before output,
// and use a number format during calculations!
Run Code Online (Sandbox Code Playgroud)
使用这个原理,作为参考,这是一个方便的小圆函数,需要精确...
function round(value, precision) {
var multiplier = Math.pow(10, precision || 0);
return Math.round(value * multiplier) / multiplier;
}
Run Code Online (Sandbox Code Playgroud)
......用法......
round(12345.6789, 2) // 12345.68
round(12345.6789, 1) // 12345.7
Run Code Online (Sandbox Code Playgroud)
...默认为舍入到最接近的整数(精度0)...
round(12345.6789) // 12346
Run Code Online (Sandbox Code Playgroud)
...并可用于舍入到最接近的10或100等...
round(12345.6789, -1) // 12350
round(12345.6789, -2) // 12300
Run Code Online (Sandbox Code Playgroud)
......并正确处理负数......
round(-123.45, 1) // -123.4
round(123.45, 1) // 123.5
Run Code Online (Sandbox Code Playgroud)
...并且可以与toFixed一起格式化为字符串...
round(456.7, 2).toFixed(2) // "456.70"
Run Code Online (Sandbox Code Playgroud)
Pab*_*dez 97
var number = 123.456;
console.log(number.toFixed(1)); // should round to 123.5
Run Code Online (Sandbox Code Playgroud)
Jas*_*ies 25
如果你使用,Math.round(5.01)
你会得到5
而不是5.0
.
如果你想要两个世界中最好的结合两者:
(Math.round(5.01 * 10) / 10).toFixed(1)
Run Code Online (Sandbox Code Playgroud)
您可能想为此创建一个函数:
function roundedToFixed(_float, _digits){
var rounded = Math.pow(10, _digits);
return (Math.round(_float * rounded) / rounded).toFixed(_digits);
}
Run Code Online (Sandbox Code Playgroud)
Kev*_*ary 23
lodash
有一个round
方法:
_.round(4.006);
// => 4
_.round(4.006, 2);
// => 4.01
_.round(4060, -2);
// => 4100
Run Code Online (Sandbox Code Playgroud)
文件.
来源.
Rus*_*sty 16
您可以简单地执行以下操作:
let n = 1.25
let result = Number(n).toFixed(1)
// output string: 1.3
Run Code Online (Sandbox Code Playgroud)
jim*_*mbo 11
我投票支持toFixed()
,但是,对于记录,这是另一种使用位移来将数字转换为int的方法.因此,它总是向零舍入(向下为正数,向下为负数).
var rounded = ((num * 10) << 0) * 0.1;
Run Code Online (Sandbox Code Playgroud)
但是,嘿,因为没有函数调用,所以它很快就是邪恶的.:)
这是一个使用字符串匹配的:
var rounded = (num + '').replace(/(^.*?\d+)(\.\d)?.*/, '$1$2');
Run Code Online (Sandbox Code Playgroud)
我不建议使用字符串变体,只是说.
Amr*_*Ali 10
一般来说,小数舍入是通过缩放来完成的:round(num * p) / p
简单的实现
使用以下具有中间数字的函数,您将获得预期的上舍入值,或有时取决于输入的下舍入值。
这种inconsistency
舍入可能会在客户端代码中引入难以检测的错误。
function naiveRound(num, decimalPlaces) {
var p = Math.pow(10, decimalPlaces);
return Math.round(num * p) / p;
}
console.log( naiveRound(1.245, 2) ); // 1.25 correct (rounded as expected)
console.log( naiveRound(1.255, 2) ); // 1.25 incorrect (should be 1.26)
Run Code Online (Sandbox Code Playgroud)
更好的实施
通过将数字转换为指数表示法的字符串,正数将按预期四舍五入。但是,请注意负数的舍入方式与正数的舍入方式不同。
事实上,它执行的规则基本上相当于“四舍五入” ,您会看到即使round(-1.005, 2)
计算结果为,但计算结果为。lodash _.round方法使用了这种技术。-1
round(1.005, 2)
1.01
/**
* Round half up ('round half towards positive infinity')
* Uses exponential notation to avoid floating-point issues.
* Negative numbers round differently than positive numbers.
*/
function round(num, decimalPlaces) {
num = Math.round(num + "e" + decimalPlaces);
return Number(num + "e" + -decimalPlaces);
}
// test rounding of half
console.log( round(0.5, 0) ); // 1
console.log( round(-0.5, 0) ); // 0
// testing edge cases
console.log( round(1.005, 2) ); // 1.01
console.log( round(2.175, 2) ); // 2.18
console.log( round(5.015, 2) ); // 5.02
console.log( round(-1.005, 2) ); // -1
console.log( round(-2.175, 2) ); // -2.17
console.log( round(-5.015, 2) ); // -5.01
Run Code Online (Sandbox Code Playgroud)
如果您希望在舍入负数时采用通常的行为,则需要在调用Math.round()之前将负数转换为正数,然后在返回之前将它们转换回负数。
// Round half away from zero
function round(num, decimalPlaces) {
num = Math.round(Math.abs(num) + "e" + decimalPlaces) * Math.sign(num);
return Number(num + "e" + -decimalPlaces);
}
Run Code Online (Sandbox Code Playgroud)
有一种不同的纯数学技术来执行舍入到最接近的值(使用“远离零的舍入一半”),其中在调用舍入函数之前应用epsilon 校正。
简而言之,我们在四舍五入之前将尽可能小的浮点值(= 1.0 ulp;最后一位的单位)添加到数字中。这将移动到数字之后的下一个可表示的值,远离零。
// Round half away from zero
function round(num, decimalPlaces) {
num = Math.round(Math.abs(num) + "e" + decimalPlaces) * Math.sign(num);
return Number(num + "e" + -decimalPlaces);
}
Run Code Online (Sandbox Code Playgroud)
这是为了抵消十进制数编码过程中可能出现的隐式舍入误差,特别是那些最后一个小数位为“5”的数字,例如 1.005、2.675 和 16.235。实际上,1.005
在十进制系统中被编码为1.0049999999999999
64位二进制浮点数;而1234567.005
在十进制系统中,则被编码为1234567.0049999998882413
64 位二进制浮点数。
值得注意的是,最大二进制round-off error
取决于 (1) 数字的大小和 (2) 相对机器 epsilon (2^-52)。
var num = 34.7654;
num = Math.round(num * 10) / 10;
console.log(num); // Logs: 34.8
Run Code Online (Sandbox Code Playgroud)
试试这个:
var original=28.453
// 1.- round "original" to two decimals
var result = Math.round (original * 100) / 100 //returns 28.45
// 2.- round "original" to 1 decimal
var result = Math.round (original * 10) / 10 //returns 28.5
// 3.- round 8.111111 to 3 decimals
var result = Math.round (8.111111 * 1000) / 1000 //returns 8.111
Run Code Online (Sandbox Code Playgroud)
更简单,更容易实施......
有了这个,你可以创建一个函数来做:
function RoundAndFix (n, d) {
var m = Math.pow (10, d);
return Math.round (n * m) / m;
}
Run Code Online (Sandbox Code Playgroud)
function RoundAndFix (n, d) {
var m = Math.pow (10, d);
return Math.round (n * m) / m;
}
console.log (RoundAndFix(8.111111, 3));
Run Code Online (Sandbox Code Playgroud)
编辑:请参阅此如何使用ROUND HALF UP进行舍入.舍入模式,我们大多数人都是在小学里教的
为什么不只是
let myNumber = 213.27321;
+myNumber.toFixed(1); // => 213.3
Run Code Online (Sandbox Code Playgroud)
小智 5
使用 toPrecision 方法:
var a = 1.2345
a.toPrecision(2)
// result "1.2"
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
318745 次 |
最近记录: |