Change comma to dot, working with decimals in php

$product['protein'] = str_replace(',', '.', str_replace('.', '', $product['protein']));

Try PHP's function str_replace():

$product['protein'] = str_replace(',', '.', $product['protein']);

Which should be a good fit.

You could think to use number_format():

number_format($value, $numberOfDecimals, $charaForDecimalPoint, $charForThousandsSeparator)

but in your case it wouldn't apply, due to the fact that your starting value ("0,3") wouldn't be recognized as a number. In fact, the decimal point for a numeric value must be a dot(".").

Use number_format only if your starting value is a true number.


$product['protein'] = str_replace(",",".",$product['protein'])

For more info on the str_replace function visit: http://uk.php.net/str_replace


try

$product['protein'] = str_replace(",",".",$product['protein'])

Tags:

Php