Well I have the following variable.
$valor = "235.33333333333333";
I need to return the value with only 2 decimal places (235.33). How can I do this? Using the explode?
Well I have the following variable.
$valor = "235.33333333333333";
I need to return the value with only 2 decimal places (235.33). How can I do this? Using the explode?
Using explode
you can do this:
$valor = "235.33333333333333";
$s = explode('.' $valor);
$result = $s[0] . '.' . substr($s[1], 0, 2);
print_r($result); // 235.33
But I recommend you do with number_format .
With number_format
:
$valor = "235.33333333333333";
$result = number_format($valor, 2);
print_f($result); // 235.33
Another option would be to use the round.
$valor = "235.33333333333333";
echo round($valor, 2); #235.33