I want to use JavaScript to restrict input on a text box to currency number formatting. For example:
<input type="text" value="2,500.00" name="rate" />
As given above, the text box should accept only numbers, commas and a period.
But, after the period it should accept two digits after numbers.
How do I accomplish this? Please clarify.
parseFloat can convert a string to a float and toFixed can set how many decimal places to keep.
function numToCurrency(num){
return parseFloat(num).toFixed(2);
}
numToCurrency("4.2334546") // returns 4.23