JavaScript Format Number To Currency


JavaScript Format Number To Currency : There are many ways to format number to currency. Here in this tutorial we are going to explain how you can format number to currency using the javascript functions and other possible solutions. We will create demo for each example you can try each example to see the output.


JavaScript Format Number To Currency Example

You format number to currency using the below methods.

You can use javascript function .toFixed(decimalPoints) to round up the number upto the given decimal points. Here is a simple example –

JavaScript Format Number To Currency Example:

<html>
<head>
<title>Javascript Demo</title>
<script type="text/javascript">
function formatNumberToMoney(inputNumber, currency) {
   var money = inputNumber.toFixed(2); // format number to two decimal digits
   document.getElementById("output").innerHTML = currency+""+money;
}
</script>
</head>
<body>
<a href="javascript:void(0)" onclick="formatNumberToMoney(98.402, '$');">Click to Format</a>
<p>Output for  98.402 = <span id="output"></span></p>
</body>
</html>

Try it »

The above function accepts the two parameters – first number to be converted and second currency symbol. If you run the above example it will produce output something like this –

JavaScript Format Number To Currency Money


Advertisements

Add Comment

📖 Read More