Show 2 digits max after floating point ... only if it is a floating point number with more than two float digits

in my application i do some math and the result can be float or int

I want to show the final result with two digits after the decimal point max ... if the result is a floating point number

There are two options:

number_format($final ,2); 

and

 sprintf ("%.2f", $final ); 

but the problem is ... if my end result is int, like 25 I end up with

 25.00 

or if the end result is something like 12.3 , it gives me

 12.30 

and i don't want

Is there a way to format a number to show 2 digits after a floating point ONLY IF this is a floating point number with more than two digits after a decimal point? or should I do some checking before formatting my number?

+6
source share
7 answers
 <?php $number = 25; print round($number, 2); print "\n"; $number = 25.3; print round($number, 2); print "\n"; $number = 25.33; print round($number, 2); 

prints:

 25 25.3 25.33 
+19
source

I think there is no short workaround for this.
Manually check if it has two or more digits after the decimal.

How do I know if it has less than one or zero digits after a decimal number? Just multiply with 10 and check if it is an integer. If so, type the number as is. If he now uses '% .2f' to print it. A.

0
source

Yes, you should do some verification. For example, check if ceil($number) > floor($number); If you need two numbers, it will require more effort.

0
source

You need to add an if condition if it contains more than 0 digits after the point. I do not see another solution.

A simple and quick way to do this.

 $final=3.40; $decimalNbr= strlen(substr(strrchr($final, "."), 1)); $final = number_format($final,(is_float($final) ? (($decimalNbr>2) ? 2 : $decimalNbr) : 0)); echo $final; 

Keep in mind that I added another number of decimal digits before using number_format.

0
source

I do not like it.

 <?php function format($number) { return preg_replace( '~\.[0-9]0+$~', null, sprintf('%.2f', $number) ); } echo format(23), PHP_EOL; //23 echo format(23.3), PHP_EOL; //23 echo format(23.33), PHP_EOL; //23.33 
0
source

I found another option. Drop to float to remove trailing zeros:

 echo (float)sprintf("%.2f", $final); // or echo (float)number_format($final ,2); 

But these functions seem to round a number like round :

 echo sprintf("%.2f", 12.556); // 12.56 echo number_format(12.556, 2); // 12.56 

So, if you do not want this behavior to use this:

 $final = 12.556; echo (int)(($final*100))/100; // 12.55 echo (int)((12*100))/100; // 12 echo (int)((12.3*100))/100; // 12.3 echo (int)((12.34567*100))/100; // 12.34 
0
source

You can use this example

 function num_format($number,$precision=0) { $precision = ($precision == 0 ? 1 : $precision); $pow = pow(10, $precision); $value = (int)((trim($number)*$pow))/$pow; return number_format($value,$precision); } echo num_format(71730116.048758); //Output //71,730,116.04 
0
source

Source: https://habr.com/ru/post/949927/


All Articles