phpnumber-formattingnumericsanitizationdigit-separator

Remove thousands separator from numeric string


I have a variable $totalprice with number 1,072.00.

If I use int($totalprice) or number_format($totalprice), it outputs 1.00.

How can I solve this problem? I want 1072.00.


Solution

  • $totalprice = (float)str_replace(',', '', $totalprice)
    

    That will get you a normal float representation of the number. You can then apply the number_format function to that to get the desired output.

    Perhaps you should rethink your handling of numbers. How in the first place the number gets the format 1,072.00? You shouldn't do any formatting to it until outputting to the browser, so the number should always be in the 1072.00 format.