Search code examples
phpexplodesubstr

Keep the values after comma if it is not 0


I have following values

100,00
100,12
122,32
120,00
140,00
123,85

If there is 0 after comma like 100,00 then I need to have it like 100. But if it is 123,85 where there is no 0 after comma then I need to keep it as it like 123,85


Solution

  • Just use the preg_replace function:

    $result = preg_replace("/,00$/", "", $input);
    

    Change to /(,00|0)$/ if you want to change 123,10 to 123,1.