Floating point format

I want to use the number_format function in PHP. For example:

 $number = 234.51; echo number_format($number,2); 

This works for floating point numbers, but I want to use it for different numbers. If the number is decimal and has no floating points, it looks like: 145.00. How can i fix this? I mean, I want to show as many floating points as possible, not more.

+7
floating-point php number-formatting
source share
8 answers

I think I canโ€™t get what I want from number_format, so I did it and it works fine:

  public function floatNumber($number) { $number_array = explode('.', $number); $left = $number_array[0]; $right = $number_array[1]; return number_format($number, strlen($right)); } 

thanx everything for your answers.

+6
source share

Explore the printf and sprintf functions instead of number_format .

They make it possible to format the numbers as you wish.

 printf("%d", $int) 

would be suitable for a decimal integer.

 printf("%4.2f", $float) 

would be suitable for a floating point with two decimal places.

number_format seems to be designed to internationalize currency output, but I don't think you want it because you specified decimal โ€œintegersโ€.

+11
source share

I don't know a better way to do this, but you can compare the type, for example, the code below:

 $number = 234.159; if(is_float($number)) { echo number_format($number, 2); }else { echo $number; } 
+3
source share

Although it's a little quick and dirty, you can just do ...

 str_replace('.00', '', number_format($potentialFloat, 2)); 

Far from ideal, but effective.

+2
source share

I think the key problem is determining how many positions are needed. Would you define 13.01 as 13 because the first decimal was 0? Since for the printf and number format you need to know how many decimal places I donโ€™t know this will work for you.

Maybe something like this (it's a lot of functions, but it searches for the first 0, and then returns a truncated string). Yes, it is intense, but it may be the best way for you.

  function show_number ($ number, $ max = 8) {
   if (strpos ($ number, '.')) {
     $ decimal = strpos ($ number, '.');
     if (strpos ($ number, '.0')) {
       return substr ($ number, 0, $ decimal); // returns whole if zero is first
     } else {
       if (strpos (substr ($ number, $ decimal, $ max), '0')) {
         $ zero = strpos (substr ($ number, $ decimal, $ max), '0');
         return substr ($ number, 0, $ decimal + $ zero); // returns number w / 0 first zero
       } else {
         return substr ($ number, 0, $ decimal + $ max + 1);  // returns number with max places
       }
     }
   } else {
     return $ number;  // returns number if no decimals
   }
 }
+1
source share
 rtrim(number_format($number, 2), '0.'); 
+1
source share

I use the following to get rid of trailing zeros:

 // Trims a floating point number so there are no trailing zeros. For example: // 1.00 -> 1 // 1.10 -> 1.1 function trimFloatingPoint($input) { $strNum = strval($input); while(substr($strNum, -1, 1) == '0') $strNum = substr($strNum, 0, strlen($strNum) -1); // Trailing zeros are gone. How about the trailing period? if(substr($strNum, -1, 1) == '.') $strNum = substr($strNum, 0, strlen($strNum) -1); return $strNum; } 
0
source share
 $x = $x - floor($x) 

will give you the correct floating point ...

0
source share

All Articles