.toFixed is not working in my code. I am using it with .toLocaleString()
JS / Fiddle: 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">
Strings do not have a toFixed()
, only numbers do.
$('#amount').text("Total: " + value.toLocaleString("en-US", {maximumFractionDigits:2, currency:"USD", style:"currency"}));
is possibly what you're after.
toFixed()
is a Number
method. toLocaleString()
turns it into a string. You need to use toFixed()
first, then parse that back to float and use toLocaleString()
:
parseFloat(value.toFixed(2)).toLocalString('en-BR');
const num = 50023.357289357; console.log(parseFloat(num.toFixed(2)).toLocaleString());
You continue to use string methods for numbers and vice versa. Try this:
const num = 5000.3269588; console.log(num.toLocaleString(undefined, {maximumFractionDigits: 2}));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.