I need to add a comma for the number when dialing and limit 2 decimal places

I am trying to add $123,123.12a comma format and limit the decimal to two places who can help with regex. I have tried so. I have an input window now that when I enter, it comes like 1231244, but I need how $123,123.12, how to make a function to achieve this, you can help with the code.

<input type="text" value="$0.00" step="0.01"/>
+4
source share
1 answer

Please look at this code below:

var value ="1231244"
var lasttwovalue = value % 100;
var firstValue= parseInt(value.slice(0,-2))
if(lasttwovalue >50)
{
   lasttwovalue = '00';
   firstValue = firstValue + parseInt(1)
}
console.log('$'+ firstValue.toLocaleString() + '.' + lasttwovalue)

//result is $12,313.00 if value is 1231255
//result is $12,312.44 if value is 1231244

Jsfiddle working demo

0
source

Source: https://habr.com/ru/post/1665721/


All Articles