how do I remove some numbers after the comma in PHP

Asked

Viewed 4,786 times

2

How do I remove some numbers after the comma in PHP? ex: 25.523832732732... Then I want you to stay only 25.52

  • And if it were 25.52832832732732... what would be the expected return?

  • Leo Caracciolo in the case I wanted the return of at most two numbers after the comma, but I already managed to do using the function (round).

  • I know, I’m asked if the number was 25.52832832732732.. the desired return would be 25.52 or 25.53 ie consider rounding or not?

  • good thinking that would be a problem in another case yes, but in the criteria I am using will not make a difference if it is 25.52 or 25.53 so do not need to consider this rounding.

  • if you do not need to consider rounding do so echo floor(25.52832832732732*100)/100; will return 25.52

  • ok this also worked but what is the difference? I used the round($var, 2) and gave the same result.

  • note that the numbers are different, 25.5238.... and 25.5283.... (38 and 83). If you make round(25.52832832732732, 2) it will return 25,53 and not 25,52,52

  • I got it, thank you very much.

Show 3 more comments

2 answers

3


This would be a solution

You use the function round which in the first parameter receives the number and in the second receives the optional number of decimal digits to round with the default being 0

$valor = round(25.52382832732732, 2);
echo $valor;

source

  • Thanks Amadeu Antunes, I’m starting with PHP, thank you so much for the help.

2

One of the ways to do this is with the number_format, in this vein:

number_format($valor, 2);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.