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
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()
so I am trying to achieve this
I have decimal 1.00 Which when i convert to float becomes 1.0. So my question is how can i save the last zero and at the same time to be float not a string if i use number_format(1.0,2) it becomes "1.00" Which is string but i need to be decimal with the last zero. This is what i tried so far
<?php
$decimal = "1.0";
$decimalToFloat = floatval($decimal) // It becomes 1.0
$decimalToFloat = number_format($decimal,2) // It becomes "1.00" !!! String not a float !!!
// The result which i want is 1.00 not "1.00"
Let me start by saying that this is an XY problem, as you don't need to convert from a string to an integer in PHP, as PHP will coerce the type for you automatically.
In addition to this, there is no need to specify the number of places on a decimal, as 1 is exactly equal to 1.0, and 1.0 is exactly equal to 1.00. In fact, forcibly casting the string showcases that PHP will automatically trim the decimal places:
$decimal = "1.0";
echo number_format((float)$decimal, 2, '.', ''); // "1.00" (string)
echo (int)number_format((float)$decimal, 2, '.', ''); // 1 (int)
echo (float)number_format((float)$decimal, 2, '.', ''); // 1 (float)
It is impossible to showcase an int or float with decimal places comprised purely of zeros in PHP. Though you do not need to do this; you can simply use the whole numbers instead:
echo 1 + 1.01; // 2.01 (float)
Hope this helps! :)
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, '.', '');
$sum = 000000000117800;
$sum = number_format($sum, 2, ".", ".");
I am looking for the output 1 178.
The sum is always a length of 15. Like assigned above. The sum could also be:
000000001117800;
Then it would be
11 178
and so on.
The last two defines the decimals: So for example
000000000117805;
would be
1 178,05
My code above gives me: 79.00 though...
I am wondering if I could solve this perhaps with ltrim, but perhaps there is another more better way ?
Your code is correct, you simply have to use a string
$sum = "000000000117800";
echo number_format($sum, 2, ".", ".");
prints ( http://ideone.com/534ATu )
117.800.00
Although, you have to divide by 100 to get the fractional part right
$sum = "000000000117800";
echo number_format($sum / 100, 2, ".", ".");
which prints ( http://ideone.com/A5LFpS )
1.178.00
the problem is when you assign
$sum = 000000000117800;
it is already being converted to 79 (as an octal value of $sum) and you cannot do anything about it.
so why do strings work? Because number_format expetcs float as a first argument, so PHP performs conversion from string to float, which ignores leading 0's. As a result we have 117800 which we have to further divide by 100 to get decimals right, then we can use number_format for nice display, or simply do (http://ideone.com/IeBCmF)
$sum = "000000000117800";
echo $sum / 100;
and get
1178
and for
$sum = "000000000117805";
echo $sum / 100;
we get
1178.05
as requested
One final remark about number_format - to get desired formatting you should use
number_format( $value, 2, ',', ' ')
so (http://ideone.com/yDQXde)
$sum = "000000000117805";
echo number_format( $sum / 100, 2, ',', ' ');
prints
1 178,05
It looks like you need to remove the leading 0's before hand. I went to http://writecodeonline.com/php/ and ran the code
$sum = 000000000117800;
$sum = number_format($sum, 2, ",", ".");<br>
echo $sum;
Then ran the code
$sum = 117800;
$sum = number_format($sum, 2, ",", ".");
echo $sum;
and it worked, it;s like the leading 0's are causing the numbers to be interpreted as another format (OCTAL as a commenter said) and not decimal.EDIT:
As a solution try ltrim before running the number_format to remove leading0'shttp://php.net/manual/en/function.ltrim.php string ltrim ( string $str [, string $charlist ] )
Strip whitespace (or other characters) from the beginning of a string.
That is because you number is treated like octal because of leading zero.
0117 in octal system is exactly 79 in decimal system.
Your initial value 000000000117800 is reduced to 0117 because 8 is not octal digit, so PHP cuts the last digits of your number.
Your output is 79 for two reasons:
Numbers starting with a leading zero are octal, octal 0117 is 7*1 + 1*8 + 1*64 == 79
PHP is broken, and will blindly ignore invalid characters at the end
Why does your number contain those leading zeros? A number has no concept of "length" - that's a property of it's string representation.
I have some double fields in my database and when echoing the fields out in my php I get .00 at the end of the values.
How do I get the .00 not to display, but display if there is a value?
You can use str_replace to remove the ".00" from the values.
$value = 10.00;
echo str_replace('.00', '', $value); // 10
$value = 10.52;
echo str_replace('.00', '', $value); // 10.52
echo (int)$double;
will simply strip off the decimal places. if you merely want to hide 'zero' decimals (10.00 -> 10), but leave non-zero decimals (10.1 -> 10.1), then you'd need to do some processing:
echo preg_replace('/\.0+$/', '', $double);
which would handle any number of zeroes after the decimal place, but leave non-zeroes in place.
if (fmod($number, 1) == 0)
{
$number = intval($number);
}
else
{
$number = round($number, 2);
}
Or just use round() [# ideone.com]:
var_dump(round($number = 5.00, 2)); // 5
var_dump(round($number = 5.01, 2)); // 5.01
For an arbitrary number of 0s at the end of the number:
$number = rtrim($number,".0");
Examples:
Input : 1.00
Result: 1
Input : 1.25
Result: 1.25
Input : 1.40
Result: 1.4
Input : 1.234910120000
Result: 1.23491012
select number,if(number % 1 = 0,cast(number as unsigned),number)
from table