I have a value $rating that is between 0.00 and 10.00.
I need to add trailing zeroes to numbers like 3.70 (which is 3.7) and 5.00 (which is 5). Do I need some kind of if statement too to check what value $rating is and then do some kind of str_pad? I'm not sure what to do.
See the number_format function
echo number_format(5, 2); // returns 5.00
echo number_format(5.2, 2); // returns 5.20
echo number_format(5.24, 2); // returns 5.24
Try number_format, something like this:
number_format((float)$yourNumber, 2, '.', '');
Related
Is there a way with number_format() to leave out decimal places if the number is not a float/decimal?
For example, I would like the following input/output combos:
50.8 => 50.8
50.23 => 50.23
50.0 => 50
50.00 => 50
50 => 50
Is there a way to do this with just a standard number_format()?
You can add 0 to the formatted string. It will remove trailing zeros.
echo number_format(3.0, 1, ".", "") + 0; // 3
A Better Solution: The above solution fails to work for specific locales. So in that case, you can just type cast the number to float data type. Note: You might loose precision after type casting to float, bigger the number, more the chances of truncating the number.
echo (float) 3.0; // 3
Ultimate Solution: The only safe way is to use regex:
echo preg_replace("/\.?0+$/", "", 3.0); // 3
echo preg_replace("/\d+\.?\d*(\.?0+)/", "", 3.0); // 3
Snippet 1 DEMO
Snippet 2 DEMO
Snippet 3 DEMO
If you want to use whitespace here is better solution
function real_num ($num, $float)
{
if (!is_numeric($num) OR is_nan($num) ) return 0;
$r = number_format($num, $float, '.', ' ');
if (false !== strpos($r, '.'))
$r = rtrim(rtrim($r, '0'), '.');
return $r;
}
Use:
$a = 50.00;
$a = round($a, 2);
Even though the number has 2 zeros trailing it, if you round it, it won't show the decimal places, unless they have some kind of value.
So 50.00 rounded using 2 places will be 50, BUT 50.23 will be 50.23.
Unless you specify at which point to round up or down, it won't change your decimal values. So just use default round()
I try to remove zero number from php variable using php function, but not work.
I try to use round or floor or ceil but not work.
How can I do that?
2.00 ===> 2
2.05 ===> 2.05 (not remove zero)
2.50 ===> 2.5
2.55 ===> 2.55 (not remove zero)
You must be having string variable. Please convert it to float as (float)$var and you will lose these zeros if you print the result out.
Other option is to use rtrim on string to remove the 0 and . from the end. see examples here http://php.net/rtrim
Try this:
number_format((float)$your_number, 2, '.', '');
I had the same problem. Function number_format() returns number as string so it will not remove zeros at the end.
Try this :
$list = array("2.00", "2.05", "2.50", "2.55");
foreach($list as $val)
echo (double) $val . "</br>";
Output:
2
2.05
2.5
2.55
In PHP you can cast the value to a Float Type (double, float, real), which will drop all leading or trailing zeros (after the decimal).
2.5 === (double) "2.50"
Be aware, however, that this does not format your number other than removing the 0's (this does not ensure a money format). For formatting, see number_format().
2.5 === (double) number_format('2.501', 2, '.', '');
Example:
(float) 2.00 === 2
(float) 2.05 === 2.05 // (not remove zero)
(float) 2.50 === 2.5
(float) 2.55 === 2.55 // (not remove zero)
And a proof: http://ideone.com/wimBHm
An interesting thing to note is that the test (float) 2.00 === 2 actually does not pass, this is because 2 is actually of type (int), and therefore fails the === test, however, as you can see, the output is exactly what you are looking for.
$price = '2.00';
$price2 = '2.50';
$price3 = '2.05';
function formatPrice($price)
{
/* Format number with 2 decimal places, then remove .00, then right trim any 0 */
return rtrim(str_replace('.00', '', number_format($price, 2)), '0');
}
echo formatPrice($price); //2
echo formatPrice($price2); //2.5
echo formatPrice($price3); //2.05
I want to format numbers like following
13.20 to 13.2
13.34 to 13.34
13.00 to 13
I have tried to use a combination of str_replace() and number_format() but not able to produce required result.
Please help me, if anyone have any idea.
Thanks in advance
Try using the built-in round function:
echo round(1.23456, 2);
This will return 1.23. Of course, you have to decide how many numbers after the decimal point to keep.
number_format should work:
<?php
$number = 12.345;
echo number_format($number, 1); # Produces number with one decimal precision.
?>
EDIT:
<?php
$number = 12.30;
#Strips ZEROs and decimal point from the end
echo rtrim($number, '0.'); #Result: 12.3
?>
Or just simply, this is magic :
$number + 0
// 12.30 + 0 = 12.3
Or you can cast your number to float :
echo floatval($number);
It depends on the type of variable you're using. A string 13.20 will output 13.20 but a float 13.20 will output 13.2.
All you have to do is cast it:
echo (float) '13.20'; // Will output 13.2
I'll like to format 1000 to 10.00
The PHP number_format function does not seem to be working for this.
I have tried:
$amount2 = number_format("$cost",2,"",",");
echo "$cost";
Any ideas? Is there a way I can manupulate number_format to display the results (i.e just inserting a decimal before the last two digits?
Number format will change the "." to a "," but you telling it to format ONE THOUSAND.
$cost=1000;
echo number_format($cost,2,'.',',');
//1,000.00
What you want is simply:
$cost=1000;
echo number_format($cost/100,2,'.',',');
//10.00
Is this legit for you ?
<?php
$cost=1000;
echo substr($cost, 0, 2) . "." . substr($cost, 2);//10.00
1000 and 10.00 are totally different numbers (in values). Divide by 100, then format it properly:
$cost = 1000 ;
$cost /= 100 ;
$amount2 = number_format($cost,2,".","");
echo $amount2 ;
Try this code:
$stringA= 1000;
$length=strlen($stringA);
$temp1=substr($stringA,0,$length-2);
$temp2=substr($stringA,$length-2,$length);
echo $temp1.".".$temp2; // Displays 10.00
The third parameter to number_format should be the character you want to use as a decimal point. Why are you passing an empty string? And why are you placing your number ($cost) inside a string?
Try this: echo number_format($cost,2,'.',',');
EDIT: Perhaps I misunderstood your question — if you want the number 1000 to be displayed as 10.00, just divide $cost by 100 before calling number_format().
I want to add .00 to my value.
For example:
100 will be 100.00
100.26 will be 100.26 only.
$YOUR_VALUE = 1000.25;
echo number_format($YOUR_VALUE, 2);
number_format() can be your friend
Like #Gaurav said, use the number_format() function. Simply pass it the value and the number of digits you want there to be after the decimal point:
$value = 100;
echo number_format($value, 2); //prints "100.00"
Note that by default, it will also insert commas as the thousands separator:
$value = 2013;
echo number_format($value, 2); //prints "2,013.00"
You can change the characters that are used as the decimal point and thousands separator by passing them in as the third and fourth parameters to the function:
$value = 2013;
echo number_format($value, 2, ',', ' '); //prints "2 013,00"
number_format(100, 2, '.', ' ')
you can use
round()
or
number_format()