Javascript如何将数字的输出更改为带有2位小数的美国货币

标签 javascript decimal currency

我想将数字的输出更改为保留 2 位小数的美元; JavaScript 部分如下所示:

var leagueDues = 5.00;
var CourseFees = 14.50;

var totalLeagueDues = leagueDues * 16;
var totalCourseFees = CourseFees * 16;

var totalDues = totalCourseFees + totalLeagueDues;

document.getElementById('league').value = totalLeagueDues;
document.getElementById('fee').value = totalCourseFees;
document.getElementById('total').value = totalDues;

有什么想法吗?

最佳答案

您可以使用 toFixed(2) 函数将数字固定为小数点后 2 位。

看起来像:

var leagueDues = 5.00; 
var CourseFees = 14.50; 
var totalLeagueDues = leagueDues * 16; 
var totalCourseFees = CourseFees * 16; 
var totalDues = totalCourseFees + totalLeagueDues; 

document.getElementById('league').value = totalLeagueDues.toFixed(2); 
document.getElementById('fee').value = totalCourseFees.toFixed(2); 
document.getElementById('total').value = totalDues.toFixed(2);

关于Javascript如何将数字的输出更改为带有2位小数的美国货币,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52304366/

相关文章:

jquery - Jqgrid货币格式

php - 使用php获取货币符号

javascript - 使用 Number.toLocaleString() 的货币格式差异

javascript - 使用 jQuery/JavaScript Div 到图像

javascript - XMLHttpRequest 跨源请求仅支持协议(protocol)方案依次服务器

mysql - RS.GetValue(RS.GetOrdinal ("balance")) 产生意外值

mysql - 用 mysql float 类型表示十进制数?

javascript - 如何等到元素的文本在 WebdriverJS 中发生变化?

javascript - 搜索一个词是否在数组中

mysql - 警告#1264 :out of range error in mysql