我想用 JavaScript 格式化价格。我想要一个以 afloat作为参数并返回string格式如下的函数:
float
string
"$ 2,500.00"
最好的方法是什么?
此解决方案与每个主要浏览器兼容:
const profits = 2489.8237; profits.toFixed(3) // Returns 2489.824 (rounds up) profits.toFixed(2) // Returns 2489.82 profits.toFixed(7) // Returns 2489.8237000 (pads the decimals)
您只需添加货币符号(例如"$" + profits.toFixed(2)),您将获得以美元为单位的金额。
"$" + profits.toFixed(2)
如果你需要,在每个数字之间使用,你可以使用这个函数:
,
function formatMoney(number, decPlaces, decSep, thouSep) { decPlaces = isNaN(decPlaces = Math.abs(decPlaces)) ? 2 : decPlaces, decSep = typeof decSep === "undefined" ? "." : decSep; thouSep = typeof thouSep === "undefined" ? "," : thouSep; var sign = number < 0 ? "-" : ""; var i = String(parseInt(number = Math.abs(Number(number) || 0).toFixed(decPlaces))); var j = (j = i.length) > 3 ? j % 3 : 0; return sign + (j ? i.substr(0, j) + thouSep : "") + i.substr(j).replace(/(\decSep{3})(?=\decSep)/g, "$1" + thouSep) + (decPlaces ? decSep + Math.abs(number - i).toFixed(decPlaces).slice(2) : ""); } document.getElementById("b").addEventListener("click", event => { document.getElementById("x").innerText = "Result was: " + formatMoney(document.getElementById("d").value); }); <label>Insert your amount: <input id="d" type="text" placeholder="Cash amount" /></label> <br /> <button id="b">Get Output</button> <p id="x">(press button to get output)</p>
像这样使用它:
(123456789.12345).formatMoney(2, ".", ",");
如果你总是要使用’.’ 和’,’,您可以将它们从您的方法调用中删除,并且该方法将为您默认它们。
(123456789.12345).formatMoney(2);
如果您的文化有两个符号翻转(即欧洲人)并且您想使用默认值,只需在formatMoney方法中粘贴以下两行:
formatMoney
d = d == undefined ? "," : d, t = t == undefined ? "." : t,
如果你可以使用现代 ECMAScript 语法(即通过 Babel),你可以使用这个更简单的函数来代替:
function formatMoney(amount, decimalCount = 2, decimal = ".", thousands = ",") { try { decimalCount = Math.abs(decimalCount); decimalCount = isNaN(decimalCount) ? 2 : decimalCount; const negativeSign = amount < 0 ? "-" : ""; let i = parseInt(amount = Math.abs(Number(amount) || 0).toFixed(decimalCount)).toString(); let j = (i.length > 3) ? i.length % 3 : 0; return negativeSign + (j ? i.substr(0, j) + thousands : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands) + (decimalCount ? decimal + Math.abs(amount - i).toFixed(decimalCount).slice(2) : ""); } catch (e) { console.log(e) } }; document.getElementById("b").addEventListener("click", event => { document.getElementById("x").innerText = "Result was: " + formatMoney(document.getElementById("d").value); }); <label>Insert your amount: <input id="d" type="text" placeholder="Cash amount" /></label> <br /> <button id="b">Get Output</button> <p id="x">(press button to get output)</p>
好的,根据你所说,我正在使用这个:
var DecimalSeparator = Number("1.2").toLocaleString().substr(1,1); var AmountWithCommas = Amount.toLocaleString(); var arParts = String(AmountWithCommas).split(DecimalSeparator); var intPart = arParts[0]; var decPart = (arParts.length > 1 ? arParts[1] : ''); decPart = (decPart + '00').substr(0,2); return '£ ' + intPart + DecimalSeparator + decPart;
我愿意接受改进建议(我不想仅仅为了这样做而包含YUI :-))
我已经知道我应该检测“。” 而不是仅仅使用它作为小数分隔符......