English 中文(简体)
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.

最佳回答

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.

问题回答

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.

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;
}

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.

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
  }
}

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;
}
$x = $x - floor($x)

will give you the right floating point...





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签