Javascript-四舍五入一个数字
此函数的结果是5.4。。。但我需要5.40英镑。Javascript-四舍五入一个数字,javascript,math,rounding,Javascript,Math,Rounding,此函数的结果是5.4。。。但我需要5.40英镑。 这是一个简单的问题,但我没有(干净的)想法来解决这个问题 JS var test = round(-5.401826873479466,2); alert(test+' $'); // = 5.4 $ function round(number, decimals) { return Math.round(number * Math.pow(10, decimals)) / Math.pow(10, decimals); }
这是一个简单的问题,但我没有(干净的)想法来解决这个问题 JS
var test = round(-5.401826873479466,2);
alert(test+' $'); // = 5.4 $
function round(number, decimals) {
return Math.round(number * Math.pow(10, decimals)) / Math.pow(10, decimals);
}
小提琴
假设这是用于显示字符串,那么您需要:
这将返回一个字符串。在数字的JS编码中,
5.4
和5.40
之间没有区别 假设这是为了显示字符串,那么您需要:
这将返回一个字符串。在数字的JS编码中,
5.4
和5.40
之间没有区别 假设这是为了显示字符串,那么您需要:
这将返回一个字符串。在数字的JS编码中,
5.4
和5.40
之间没有区别 假设这是为了显示字符串,那么您需要:
这将返回一个字符串。在数字的JS编码中,5.4
和5.40
之间没有区别 试试这个
var test = round(-5.401826873479466,2);
test=test.toFixed(2);
alert(test+' $');
function round(number, decimals) {
return Math.round(number * Math.pow(10, decimals)) / Math.pow(10, decimals);
}试试这个
var test = round(-5.401826873479466,2);
test=test.toFixed(2);
alert(test+' $');
function round(number, decimals) {
return Math.round(number * Math.pow(10, decimals)) / Math.pow(10, decimals);
}试试这个
var test = round(-5.401826873479466,2);
test=test.toFixed(2);
alert(test+' $');
function round(number, decimals) {
return Math.round(number * Math.pow(10, decimals)) / Math.pow(10, decimals);
}试试这个
var test = round(-5.401826873479466,2);
test=test.toFixed(2);
alert(test+' $');
function round(number, decimals) {
return Math.round(number * Math.pow(10, decimals)) / Math.pow(10, decimals);
}然后,OP总是可以直接使用。然后,OP总是可以直接使用。然后,OP总是可以直接使用。然后,OP总是可以直接使用。然后,OP总是可以直接使用。