I am making a currency converter, and I would like the result to have proper comma formatting as well as ALWAYS rounding to two decimals.
For example, if my number is 1000000.3, I would like the result to be 1,000,000.30.
Is there anything that I am missing? I am very new to coding so any help would be appreciated!
EDIT: not seeing that stack overflow is providing an option to run the code, so you could either copy and paste it into a code editor or open the code here: https://xrpalerts.000webhostapp.com/testing.html
<!DOCTYPE html>
<html>
<body>
<script>
var CalculatorResult = 1000000.3
var CalculatorResultLocale = parseFloat(CalculatorResult).toLocaleString("us-EN");
var CalculatorResultLocaleFixed = parseFloat(CalculatorResultLocale).toFixed(2);
var CalculatorResultFixed = parseFloat(CalculatorResult).toFixed(2);
var CalculatorResultFixedLocale = parseFloat(CalculatorResultFixed).toLocaleString("us-EN");
function ClickAllResults() {
document.write("CalculatorResultLocale: ");
document.write(CalculatorResultLocale);
document.write("<br>");
document.write("CalculatorResultLocaleFixed: ");
document.write(CalculatorResultLocaleFixed);
document.write("<br>");
document.write("<br>");
document.write("CalculatorResultFixed: ");
document.write(CalculatorResultFixed);
document.write("<br>");
document.write("CalculatorResultFixedLocale: ");
document.write(CalculatorResultFixedLocale);
document.write("<br>");
}
</script>
<button onclick="ClickAllResults()" id = "button"><strong>Click to see all results</strong></button>
</body>
</html>
To display toLocaleString by decimal place use
.toLocaleString("us-EN", { minimumFractionDigits: 2, maximumFractionDigits: 2 })
you`r code will be like this
var CalculatorResultFixedLocale = parseFloat(CalculatorResultFixed).toLocaleString("us-EN", { minimumFractionDigits: 2, maximumFractionDigits: 2 });