Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Floating point number format

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

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

This works for float numbers but I want to use it for different numbers. If a number is decimal and doesn't have any floating points, it shows like : 145.00 . How can I fix this? I mean I want to show as many floating points as needed, not more.

like image 808
AliBZ Avatar asked Nov 14 '09 13:11

AliBZ


People also ask

How do you write a floating-point number?

Like scientific notation, floating-point numbers have a sign, mantissa (M), base (B), and exponent (E), as shown in Figure 5.27. For example, the number 4.1 × 103 is the decimal scientific notation for 4100. It has a mantissa of 4.1, a base of 10, and an exponent of 3.

What is the floating-point of a number?

A floating point number, is a positive or negative whole number with a decimal point. For example, 5.5, 0.25, and -103.342 are all floating point numbers, while 91, and 0 are not. Floating point numbers get their name from the way the decimal point can "float" to any position necessary.

What is IEEE floating point format?

IEEE single-precision floating-point format. The format of IEEE single-precision floating-point standard representation requires 23 fraction bits F, 8 exponent bits E, and 1 sign bit S, with a total of 32 bits for each word. F is the mantissa in 2's complement positive binary fraction represented from bit 0 to bit 22.


5 Answers

Investigate the printf and sprintf functions instead of number_format.

They give the ability to format numbers as you wish.

printf("%d", $int) 

would be appropriate for a decimal integer.

printf("%4.2f", $float) 

would be appropriate for a floating point number with two decimal places.

number_format seems to be intended for internationalisation of currency output, but I don't think that's what you want because you mentioned decimal 'whole' numbers.

like image 86
pavium Avatar answered Oct 21 '22 18:10

pavium


I think I can't get what I want from number_format so I did this 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 all for your replies.

like image 31
AliBZ Avatar answered Oct 21 '22 18:10

AliBZ


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

$number = 234.159;
if(is_float($number)) {
 echo number_format($number, 2);
}else {
 echo $number;
}
like image 22
Lucas Renan Avatar answered Oct 21 '22 19:10

Lucas Renan


Whilst it's a bit quick and dirty, you could simply do a...

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

Far from ideal, but effective.

like image 38
John Parker Avatar answered Oct 21 '22 20:10

John Parker


I think the key problem is defining how many positions are needed. Would you define 13.01 as 13 because the first decimal was a 0? Since printf and number format needs you to know how many decimals, I don't know that that would work for you.

Maybe something like this (which is a lot of functions, but looks for the first 0, and then returns the truncated string). Yes, it is intensive, 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
  }
}
like image 41
Cryophallion Avatar answered Oct 21 '22 18:10

Cryophallion