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()
Related
I am looking for a solution for a smart number formatting in PHP.
For example we have 2 numbers below and we need 4 digits after decimal:
1.12345678
0.00002345678
Using normal number formatting, here are the results:
1.1234 // Looking good
0.0000 // No good
Can we make it keep going until there are 4 non-zero digits? If it can return 0.00002345, perfect!!!
Many thanks!!!
Might be overkill and the pattern could be optimized, but for fun; get optional 0s AND 4 NOT 0s after the .:
preg_match('/\d+\.([0]+)?[^0]{4}/', $num, $match);
echo $match[0];
To round it we can get 5 digits after the 0s and then format it to the length of that -1 (which will round):
preg_match('/\d+\.([0]+?[^0]{5})/', $num, $match);
echo number_format($match[0], strlen($match[1])-1);
For $num = '1234.000023456777'; the result will be 1,234.00002346 and the $matches will contain:
Array
(
[0] => 1234.000023456
[1] => 000023456
)
So this is the code I made to slove this:
$num = 0.00002345678;
$num_as_string = number_format($num,PHP_FLOAT_DIG,'.','');
$zeros = strspn($num_as_string, "0", strpos($num_as_string, ".")+1);
echo number_format($num, (4+$zeros), '.', '');
It converts the float number to a string, checks how many zeros exist after the decimal point and then does a number format with the extra zeros accounted for.
Note that it may break if your float is too big, you can change PHP_FLOAT_DIG to a number larger that may fix that.
Is there a built-in/neat way to format a number (just like number_format does), but without any rounding ups/downs?
For instance, number 1234.234 should be formatted as 1,234.234 and another number 1234 should be formatted as 1,234 (i.e. without any trailing .000)
You can define simple custom function for that:
<?php
function custom_number_format($number, $decimal = '.')
{
$broken_number = explode($decimal, $number);
if (isset($broken_number[1]))
return number_format($broken_number[0]) . $decimal . $broken_number[1];
else
return number_format($broken_number[0]);
}
$n1 = '1234.234';
$n2 = '1234';
echo custom_number_format($n1);
echo '<br>';
echo custom_number_format($n2);
?>
Output is:
1,234.234
1,234
Based on the arhey's answer
TLDR ;)
You can use number_format to format the number to a fixed-width format, then use rtrim twice to remove trailing zeroes, and dot.
rtrim(rtrim(number_format($number, 3, '.', ','), '0'), '.')
Starting from the last character, rtrim removes it while it is one of those given. In our case, we remove trailing dots, then we remove an eventual trailing zero.
rtrim(rtrim(number_format(1234.123, 3, '.', ','), '0'), '.')
// returns 1,234.123
rtrim(rtrim(number_format(1234.12, 3, '.', ','), '0'), '.')
// returns 1,234.12 (1,234.120, trimmed to 1234.12)
rtrim(rtrim(number_format(1234, 3, '.', ','), '0'), '.')
// returns 1,234 (1,234.000, trimmed to 1234)
rtrim(rtrim(number_format(1200, 3, '.', ','), '0'),'.')
// returns 1,200 (1,200.000, trimmed to 1200., trimmed to 1200)
Formal form, and discussion about the parameters (notably the decimals count)
rtrim(rtrim(number_format($number, <N>, '<D>', ''), '0'), '<D>')
Where :
D is the decimal separator. To avoid locale-formatting problems, explicitly specify it
N is the maximum digits you number can have.
If you know all your numbers will have less than 3 digits, go and take N=3.
What if you don't know how many decimals are at most ? Well, things are getting more complex.
It may worth recalling (as stated in the PHP documentation) that floats are stored :
with a precision (a number of digits, without distinction whether they are before or after the decimal separator), not a number of decimals
and in their binary form, not their decimal one, and that can lead to rounding errors when reaching precision limit.
For example, floor((0.1+0.7)*10) will usually return 7 instead of the
expected 8, since the internal representation will be something like
7.9999999999999991118....
So there is no universal good value, you'll have to choose it depending on the usual scale of your data.
And that explains why there is no built-in function for that : PHP can't choose for you.
You can use function:
<?php
function getNumberFormat($number) {
$numberAr = explode('.', (string)$number);
$count = 0;
if (2 === count($numberAr)) {
$count = strlen($numberAr[1]);
}
return number_format($number, $count, ',', '.');
}
$test1 = 1234.234;
$test2 = 1234;
echo getNumberFormat($test1); //1,234.234
echo getNumberFormat($test2); //1,234
I really liked arhey's answer, but later realized it has a major flaw. A number like 2100 will get converted to 2,1 instead of 2,100.
Below is how I ended up modifying it.
public function formatDecimal($number)
{
$stringVal = strval($number); //convert number to string
$decPosition = strpos($stringVal, ".");
if ($decPosition !== false) //there is a decimal
{
$decPart = substr($stringVal, $decPosition); //grab only the decimal portion
$result = number_format($stringVal) . rtrim($decPart, ".0");
}
else //no decimal to worry about
{
$result = number_format($stringVal);
}
return $result;
}
It's not as succinct a solution as I was hoping, but in my case I put it into a view helper (I'm using ZF2) and so it's just one simple function call in my view.
Hope this is helpful for someone!
rtrim(number_format(1234.234, 3),'.0');
rtrim(number_format(1234, 3),'.0');
Let's begin with that there's no decimal type in PHP. There's float only.
And if you know how float works, then you know that it's usually not possible to store exact decimal value that you think you have, but it's an approximation. That's because you can't express most of decimal numbers in binary system.
Therefore if you say:
$number = 1234.234;
Then you have a float that is close to this value. The real value is:
1234.23399999999992360244505107402801513671875
Therefore PHP can't just guess how do you want to round it. It needs to be specified explicitly.
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
$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 know of the PHP function floor() but that doesn't work how I want it to in negative numbers.
This is how floor works
floor( 1234.567); // 1234
floor(-1234.567); // -1235
This is what I WANT
truncate( 1234.567); // 1234
truncate(-1234.567); // -1234
Is there a PHP function that will return -1234?
I know I could do this but I'm hoping for a single built-in function
$num = -1234.567;
echo $num >= 0 ? floor($num) : ceil($num);
Yes intval
intval(1234.567);
intval(-1234.567);
Truncate floats with specific precision:
echo bcdiv(2.56789, 1, 1); // 2.5
echo bcdiv(2.56789, 1, 3); // 2.567
echo bcdiv(-2.56789, 1, 1); // -2.5
echo bcdiv(-2.56789, 1, 3); // -2.567
This method solve the problem with round() function.
Also you can use typecasting (no need to use functions),
(int) 1234.567; // 1234
(int) -1234.567; // -1234
http://php.net/manual/en/language.types.type-juggling.php
You can see the difference between intval and (int) typecasting from here.
another hack is using prefix ~~ :
echo ~~1234.567; // 1234
echo ~~-1234.567; // 1234
it's simpler and faster
Tilde ~ is bitwise NOT operator in PHP and Javascript
Double tilde(~) is a quick way to cast variable as integer, where it is called 'two tildes' to indicate a form of double negation.
It removes everything after the decimal point because the bitwise operators implicitly convert their operands to signed 32-bit integers. This works whether the operands are (floating-point) numbers or strings, and the result is a number
reference:
https://en.wikipedia.org/wiki/Double_tilde
What does ~~ ("double tilde") do in Javascript?
you can use intval(number); but if your number bigger than 2147483648 (and your machine/os is x64) all bigs will be truncated to 2147483648. So you can use
if($number < 0 )
$res = round($number);
else
$res = floor($number);
echo $res;
You can shift the decimal to the desired place, intval, and shift back:
function truncate($number, $precision = 0) {
// warning: precision is limited by the size of the int type
$shift = pow(10, $precision);
return intval($number * $shift)/$shift;
}
Note the warning about size of int -- this is because $number is potentially being multiplied by a large number ($shift) which could make the resulting number too large to be stored as an integer type. Possibly converting to floating point might be better.
You could get fancy with a $base parameter, and sending that to intval(...).
Could (should) also get fancy with error/bounds checking.
An alternative approach would be to treat number as a string, find the decimal point and do a substring at the appropriate place after the decimal based on the desired precision. Relatively speaking, that won't be fast.