Get value after point [duplicate]

1

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?

    
asked by anonymous 31.05.2017 / 15:12

2 answers

4

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
    
31.05.2017 / 15:14
2

Another option would be to use the round.

$valor = "235.33333333333333";
echo round($valor, 2); #235.33
    
31.05.2017 / 15:38