我一直在做一個專案,在 UI 中發現一個帶有 JS 訊息的錯誤。該數字在顯示之前不會四舍五入。我不太了解 JS 但是,我確實嘗試過Math.round(num * 100) / 100這沒有用。
對此的任何支持都會很棒。
if($change_return_val != "0.00"){
// confirm('Oops amount is over.');
// calculate_balance_due();
swal({
title: "Change: £" $change_return_val,
text: "Your change return value is: £" $change_return_val,
})
}
更改 UI 中的回傳訊息
uj5u.com熱心網友回復:
將 a 轉換number為string具有指定浮點數的 a 時,正如@Ameer 在評論中所說,該toFixed函式是典型的方法。但是,對于將 a 顯示number為貨幣的特定任務,您可能需要考慮使用toLocaleString。
例如,這將數字轉換為英式英語風格,特別是作為英鎊的貨幣:
const numberRaw = 123.456789;
const numberCurrency = numberRaw.toLocaleString('en-GB', { style: 'currency', currency: 'GBP' });
console.log(numberCurrency);
如果您必須處理許多語言環境(其中一些可能以完全不同的方式顯示數字,例如'1.234,57'.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/362462.html
標籤:javascript
