How to round a number when it has both commas and decimals

Options

I have a number which I want to round to the nearest whole number such as 99,111,222.83. When I try the action "Field: Set Value of a Decimal Field" and set the decimal places to 0, the number rounds to just 99 and truncates everything after the first comma. I would expect this to output 99,111,223 and have also tried changing the number of decimal places from 0 to 2 to 6 and still it truncates and only returns 99. How can I best handle the comma delimiter that is causing this to not round properly?

Best Answer

Answers

  • Thomas_937381
    Options

    @Nasir_204951 I would try removing the commas with a formula, then round.

  • Nasir_168130
    Options

    thanks, @Thomas_937381. I tried this parseFloat('{{filed}}'.replace(/,/g, '')) to remove commas from 99,456,376.71 and it did not remove the commas and returned the number as is. I then tried parseInt('{{filed}}'.replace(/,/g, '')) and it ended up just removing the decimal point returning 99,456,376. This would be ok, except it needs to round the number then to 99,456,377. Any ideas on which formula to use?

  • Thomas_937381
    Thomas_937381 Posts: 196
    edited July 2020
    Options

    Hey @Nasir_204951.

    I tried this:

    var x = fields['field-ref'].toString().replace(/,/g,"")
    result = parseInt(x)
    

    I notice that the first bit gives me a text string without the commas, and after the parseFloat method, it's again displaying with commas. I wonder if the commas might just be a regional display thing in the browser.

    Edit: You should be able to use parseInt to round up.

    Thomas