Javascript 你如何在Javascript中四舍五入到小数点后一位?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7342957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you round to 1 decimal place in Javascript?
提问by Walker
Can you round a number in javascript to 1 character after the decimal point (properly rounded)?
您可以将 javascript 中的数字四舍五入到小数点后的 1 个字符(正确四舍五入)吗?
I tried the *10, round, /10 but it leaves two decimals at the end of the int.
我尝试了 *10, round, /10 但它在 int 的末尾留下了两位小数。
回答by Billy Moon
Math.round(num * 10) / 10
works, here is an example...
Math.round(num * 10) / 10
有效,这是一个例子......
var number = 12.3456789
var rounded = Math.round(number * 10) / 10
// rounded is 12.3
if you want it to have one decimal place, even when that would be a 0, then add...
如果你希望它有一个小数位,即使那是 0,然后加上......
var fixed = rounded.toFixed(1)
// fixed is always to 1 d.p.
// NOTE: .toFixed() returns a string!
// To convert 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!
EDIT: Add round with precision function...
编辑:添加具有精度功能的圆形...
Using this principle, for reference, here is a handy little round function that takes precision...
使用这个原理,作为参考,这里有一个方便的小圆函数,它需要精确......
function round(value, precision) {
var multiplier = Math.pow(10, precision || 0);
return Math.round(value * multiplier) / multiplier;
}
... usage ...
... 用法 ...
round(12345.6789, 2) // 12345.68
round(12345.6789, 1) // 12345.7
... defaults to round to nearest whole number (precision 0) ...
... 默认四舍五入到最接近的整数(精度 0) ...
round(12345.6789) // 12346
... and can be used to round to nearest 10 or 100 etc...
...并可用于四舍五入到最接近的 10 或 100 等...
round(12345.6789, -1) // 12350
round(12345.6789, -2) // 12300
... and correct handling of negative numbers ...
... 以及正确处理负数 ...
round(-123.45, 1) // -123.4
round(123.45, 1) // 123.5
... and can be combined with toFixed to format consistently as string ...
...并且可以与 toFixed 结合使用以一致地格式化为字符串 ...
round(456.7, 2).toFixed(2) // "456.70"
回答by Pablo Fernandez
var number = 123.456;
console.log(number.toFixed(1)); // should round to 123.5
回答by Jasper de Vries
If you use Math.round(5.01)
you will get 5
instead of 5.0
.
如果您使用,Math.round(5.01)
您将得到5
而不是5.0
.
If you use toFixed
you run into roundingissues.
If you want the best of both worlds combine the two:
如果您想要两全其美,请将两者结合起来:
(Math.round(5.01 * 10) / 10).toFixed(1)
You might want to create a function for this:
您可能想为此创建一个函数:
function roundedToFixed(_float, _digits){
var rounded = Math.pow(10, _digits);
return (Math.round(_float * rounded) / rounded).toFixed(_digits);
}
回答by Kevin Leary
回答by jimbo
I vote for toFixed()
, but, for the record, here's another way that uses bit shifting to cast the number to an int. So, it always rounds towards zero (down for positive numbers, up for negatives).
我投票给toFixed()
,但是,为了记录,这是另一种使用位移将数字转换为 int 的方法。因此,它总是向零舍入(正数向下,负数向上)。
var rounded = ((num * 10) << 0) * 0.1;
But hey, since there are no function calls, it's wicked fast. :)
但是,嘿,因为没有函数调用,所以速度非常快。:)
And here's one that uses string matching:
这是使用字符串匹配的一个:
var rounded = (num + '').replace(/(^.*?\d+)(\.\d)?.*/, '');
I don't recommend using the string variant, just sayin.
我不建议使用字符串变体,只是说。
回答by atiruz
Try with this:
试试这个:
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
less complicated and easier to implement...
更简单,更容易实现......
with this, you can create a function to do:
有了这个,你可以创建一个函数来做:
function RoundAndFix (n, d) {
var m = Math.pow (10, d);
return Math.round (n * m) / m;
}
function RoundAndFix (n, d) {
var m = Math.pow (10, d);
return Math.round (n * m) / m;
}
console.log (RoundAndFix(8.111111, 3));
EDIT: see this How to round using ROUND HALF UP. Rounding mode that most of us were taught in grade school
回答by super
x = number, n = decimal-places:
x = 数字,n = 小数位数:
function round(x, n) {
return Math.round(x * Math.pow(10, n)) / Math.pow(10, n)
}
回答by goulashsoup
Why not just
为什么不只是
let myNumber = 213.27321;
+myNumber.toFixed(1); // => 213.3
- toFixed: returns a string representing the given number using fixed-point notation.
- Unary plus (+): The unary plus operator precedes its operand and evaluates to its operand but attempts to convert it into a number, if it isn't already.
回答by E.Z.
Using toPrecision method:
使用 toPrecision 方法:
var a = 1.2345
a.toPrecision(2)
// result "1.2"
回答by Joseph Silber
var num = 34.7654;
num = Math.round(num * 10) / 10;
console.log(num); // Logs: 34.8