[英]JS Two Decimal Places .toFixed(2)
.toFixed 在我的代碼中不起作用。 我將它與.toLocaleString()
JS/小提琴: https : //jsfiddle.net/8b6t90f5/
$(function() { var value = 5000.3269588; $("#process").click(function() { $('#amount').text("Total: $" + value.toLocaleString().toFixed(2)); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="amount"></div> <input id="process" class="button_text" type="submit" name="submit" value="SHOW VALUE">
字符串沒有toFixed()
,只有數字有。
$('#amount').text("Total: " + value.toLocaleString("en-US", {maximumFractionDigits:2, currency:"USD", style:"currency"}));
可能是你所追求的。
toFixed()
是一個Number
方法。 toLocaleString()
將其轉換為字符串。 您需要先使用toFixed()
,然后將其解析回 float 並使用toLocaleString()
:
parseFloat(value.toFixed(2)).toLocalString('en-BR');
const num = 50023.357289357; console.log(parseFloat(num.toFixed(2)).toLocaleString());
您繼續對數字使用字符串方法,反之亦然。 嘗試這個:
const num = 5000.3269588; console.log(num.toLocaleString(undefined, {maximumFractionDigits: 2}));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.