如何使数字具有正确的逗号格式并在 Javascript 中每次四舍五入到恰好 2 位小数?
How to make a number have proper comma formatting AND round to exactly 2 decimals every time in Javascript?
我正在制作一个货币转换器,我希望结果具有正确的逗号格式,并且始终四舍五入到两位小数。
例如,如果我的号码是 1000000.3,我希望结果是 1,000,000.30。
有什么我遗漏的吗?我对编码很陌生,所以任何帮助将不胜感激!
编辑:没有看到堆栈溢出为 运行 代码提供了一个选项,因此您可以将其复制并粘贴到代码编辑器中或在此处打开代码:https://xrpalerts.000webhostapp.com/testing.html
<!DOCTYPE html>
<html>
<body>
<script>
var CalculatorResult = 1000000.3
var CalculatorResultLocale = parseFloat(CalculatorResult).toLocaleString("us-EN");
var CalculatorResultLocaleFixed = parseFloat(CalculatorResultLocale).toFixed(2);
var CalculatorResultFixed = parseFloat(CalculatorResult).toFixed(2);
var CalculatorResultFixedLocale = parseFloat(CalculatorResultFixed).toLocaleString("us-EN");
function ClickAllResults() {
document.write("CalculatorResultLocale: ");
document.write(CalculatorResultLocale);
document.write("<br>");
document.write("CalculatorResultLocaleFixed: ");
document.write(CalculatorResultLocaleFixed);
document.write("<br>");
document.write("<br>");
document.write("CalculatorResultFixed: ");
document.write(CalculatorResultFixed);
document.write("<br>");
document.write("CalculatorResultFixedLocale: ");
document.write(CalculatorResultFixedLocale);
document.write("<br>");
}
</script>
<button onclick="ClickAllResults()" id = "button"><strong>Click to see all results</strong></button>
</body>
</html>
//execute below function
CommaFormatted('2345643.123')
//result => 2,345,643.123
function CommaFormatted(amount) {
var delimiter = ","; // replace comma if desired
var a = amount.split('.',2)
var d = a[1];
var i = parseInt(a[0]);
if(isNaN(i)) { return ''; }
var minus = '';
if(i < 0) { minus = '-'; }
i = Math.abs(i);
var n = new String(i);
var a = [];
while(n.length > 3) {
var nn = n.substr(n.length-3);
a.unshift(nn);
n = n.substr(0,n.length-3);
}
if(n.length > 0) { a.unshift(n); }
n = a.join(delimiter);
if(d.length < 1) { amount = n; }
else { amount = n + '.' + d; }
amount = minus + amount;
return amount;
}
<!DOCTYPE html>
<html>
<body>
<script>
var CalculatorResult = 1000000.3
var CalculatorResultMoney = currencyFormat(CalculatorResult);
function ClickAllResults() {
document.write("CalculatorResultMoney: ");
document.write(CalculatorResultMoney);
document.write("<br>");
}
function currencyFormat(num) {
let a = '$' + num.toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, ',')
return a;
}
</script>
<button onclick="ClickAllResults()" id = "button"><strong>Click to see all results</strong></button>
</body>
</html>
我确实找到了与您的要求类似的功能
https://blog.abelotech.com/posts/number-currency-formatting-javascript/
function currencyFormat(num) {
let a = '$' + num.toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, ',')
return a;
}
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
const round = (value, decimals) => {
return numberWithCommas(Number(Math.round(value + 'e' + decimals) + 'e-' + decimals).toFixed(2));
};
console.log(round(84347.336, 2)); // 84,347.34
console.log(round(8557.332, 2)); // 8,557.33
console.log(round(187, 2)); // 187.00
toFixed() 方法使用定点表示法格式化数字。
要按小数位显示 toLocaleString 使用
.toLocaleString("us-EN", { minimumFractionDigits: 2, maximumFractionDigits: 2 })
你的代码会像这样
var CalculatorResultFixedLocale = parseFloat(CalculatorResultFixed).toLocaleString("us-EN", { minimumFractionDigits: 2, maximumFractionDigits: 2 });
我正在制作一个货币转换器,我希望结果具有正确的逗号格式,并且始终四舍五入到两位小数。
例如,如果我的号码是 1000000.3,我希望结果是 1,000,000.30。
有什么我遗漏的吗?我对编码很陌生,所以任何帮助将不胜感激!
编辑:没有看到堆栈溢出为 运行 代码提供了一个选项,因此您可以将其复制并粘贴到代码编辑器中或在此处打开代码:https://xrpalerts.000webhostapp.com/testing.html
<!DOCTYPE html>
<html>
<body>
<script>
var CalculatorResult = 1000000.3
var CalculatorResultLocale = parseFloat(CalculatorResult).toLocaleString("us-EN");
var CalculatorResultLocaleFixed = parseFloat(CalculatorResultLocale).toFixed(2);
var CalculatorResultFixed = parseFloat(CalculatorResult).toFixed(2);
var CalculatorResultFixedLocale = parseFloat(CalculatorResultFixed).toLocaleString("us-EN");
function ClickAllResults() {
document.write("CalculatorResultLocale: ");
document.write(CalculatorResultLocale);
document.write("<br>");
document.write("CalculatorResultLocaleFixed: ");
document.write(CalculatorResultLocaleFixed);
document.write("<br>");
document.write("<br>");
document.write("CalculatorResultFixed: ");
document.write(CalculatorResultFixed);
document.write("<br>");
document.write("CalculatorResultFixedLocale: ");
document.write(CalculatorResultFixedLocale);
document.write("<br>");
}
</script>
<button onclick="ClickAllResults()" id = "button"><strong>Click to see all results</strong></button>
</body>
</html>
//execute below function
CommaFormatted('2345643.123')
//result => 2,345,643.123
function CommaFormatted(amount) {
var delimiter = ","; // replace comma if desired
var a = amount.split('.',2)
var d = a[1];
var i = parseInt(a[0]);
if(isNaN(i)) { return ''; }
var minus = '';
if(i < 0) { minus = '-'; }
i = Math.abs(i);
var n = new String(i);
var a = [];
while(n.length > 3) {
var nn = n.substr(n.length-3);
a.unshift(nn);
n = n.substr(0,n.length-3);
}
if(n.length > 0) { a.unshift(n); }
n = a.join(delimiter);
if(d.length < 1) { amount = n; }
else { amount = n + '.' + d; }
amount = minus + amount;
return amount;
}
<!DOCTYPE html>
<html>
<body>
<script>
var CalculatorResult = 1000000.3
var CalculatorResultMoney = currencyFormat(CalculatorResult);
function ClickAllResults() {
document.write("CalculatorResultMoney: ");
document.write(CalculatorResultMoney);
document.write("<br>");
}
function currencyFormat(num) {
let a = '$' + num.toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, ',')
return a;
}
</script>
<button onclick="ClickAllResults()" id = "button"><strong>Click to see all results</strong></button>
</body>
</html>
我确实找到了与您的要求类似的功能 https://blog.abelotech.com/posts/number-currency-formatting-javascript/
function currencyFormat(num) {
let a = '$' + num.toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, ',')
return a;
}
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
const round = (value, decimals) => {
return numberWithCommas(Number(Math.round(value + 'e' + decimals) + 'e-' + decimals).toFixed(2));
};
console.log(round(84347.336, 2)); // 84,347.34
console.log(round(8557.332, 2)); // 8,557.33
console.log(round(187, 2)); // 187.00
toFixed() 方法使用定点表示法格式化数字。
要按小数位显示 toLocaleString 使用
.toLocaleString("us-EN", { minimumFractionDigits: 2, maximumFractionDigits: 2 })
你的代码会像这样
var CalculatorResultFixedLocale = parseFloat(CalculatorResultFixed).toLocaleString("us-EN", { minimumFractionDigits: 2, maximumFractionDigits: 2 });