Doing an billing application in php, There come price with decimals like 0.576 tried
round()
number_format()
it will give me 0.58 but i want 0.57 only, how can i get in php?
Something like this?
floor(0.576*100)/100
//0.57
Using round with PHP_ROUND_HALF_DOWN wont work as it only affect the way decimal 5 is rounded. It does not truncate the float value.
round(0.576, 2, PHP_ROUND_HALF_DOWN);
//0.58 //Not good
round(0.575, 2, PHP_ROUND_HALF_DOWN);
//0.57 //Good but will not work above this value (e.g 0.0576, 0.0577...)
A fraction higher than .5 is always rounded up. If you already have found round why haven't you looked at the documentation:
http://www.php.net/manual/en/function.round.php
Try:
echo round( 0.676, 2, PHP_ROUND_HALF_DOWN );
Related
The sprinf behaviour below is very strange.
// The code below outputs 1639
$value = floatval('16.40') * 100;
echo sprintf('%d', $value);
But when I use %f it outputs 1640.000000
I dumped the $value, it's definitely float(1640).
Does anyone knows what happened when I use %d? Is this a PHP bug? I've tested in PHP7.
The variable $value contains a floating point number. Floating point numbers are not exact, they are approximations.1
The value of $value is not 1640 but 1639.99999999999977262632. When you use %f to print it, printf() rounds it to 6 decimal places and the rounded value is 1640.000000.
When it is printed with %d, printf() uses only the integer part of $value and it is 1639.
Use round() to get the correct integer value:
printf('%d', round($value, 0));
Or, even better, use number_format() to get the value as string:
echo(number_format($value, 0, '', ''));
Both print 1640.
1 Think of 1/3. It is approximatively 0.3333333333 but one cannot represent it exactly because it has an infinite number of 3 after the decimal dot. In mathematics, 3 * 1/3 = 1 but in real life (and in computers), 3 * 0.333333333 is never 1, no matter how many 3 we put after the decimal dot. The computer programs usually round the value to some number of decimal places and it ends up being displayed as 1 but this doesn't happen always.
When it doesn't happen, another question similar to this one pops up on StackOverflow.
I have a code in which calculates the Effective interest rate.
when I echo it I get 5.1161897881733 which I want to limit the remainder and output it like 5.11, is there any functions use to limit the remainder in php?
A billion ways to do this. The task for you here is to pick one.
Method 1 - round()
This will just round your number. The exact rules can be found in the PHP.net documentation.
round($someNumber, 2);
Method 2 - floor()
Floor will round the number down
floor($someNumber, 2);
Method 3 - ceil()
Opposite to floor() this will round your number upwards.
ceil($someNumber, 2);
Method 4 - number_format()
This will format any number. number_format() has a gazillion possible inputs in which you can choose decimal characters etc.
// Will round your number to 2 decimals with a . as decimal character
number_format($someNumber, 2, ",", ".");
Feel free to edit and add more options :)
round($result, 0)
The 0 represents the decimal places.
http://php.net/manual/en/function.round.php
Is this a bug or some math thing I don't understand?
round(58.900662, 2) => 58.9
Where is the last zero?
EDIT:
In my opinion, the documentation is lacking information.
The following code does as expected
number_format(round(58.900662, 2), 2) => 58.90
round will take care of the precision if that digit is not 0 as the last 0 has no effect in any mathematical operation.
If the number is 1.569 then round will return 1.57
If you want that 0 the you can use -
number_format(58.900662, 2, '.', ',');
number_format()
You told it to round it to the 2nd decimal place which would be 58.90. However, 58.90 can be simplified to 58.9. It is the same number.
If you want to force the display of two decimal places you can use PHP's number_format() function.
I have a question regarding number formating in PHP.
I have a variable called "average", which is simply an average of a few values. To make it clear I rounded the number to 2 decimal places. Now the problem is, that if the average is for example 2.90, it only shows 2.9. Is there any way of displaying 2 decimal places always? I though I could do it by multiplying the number by 100, rounding it to zero d.p. and then divide by 100 again, but that seems a bit overcomplicated if there is an easier way of doing it.
Maybe you can try the number_format(float $number [, int $decimals = 0 ])?
For more information, take a look at http://php.net/manual/en/function.number-format.php
Format the output with printf
printf("%.1f", $num); // prints 1 decimal place
printf("%.2f", $num); // prints 2 decimal places
I am confused as to why:
echo log10(238328) / log10(62);
results in 3
but
echo floor(log10(238328) / log10(62));
results in 2
I know floor rounds down but I thought it was only for decimal numbers.
How can I get an answer of 3 out of the latter statment whilst still normally rounding down?
PHP uses double-precision floating point numbers. Neither of the results of the two logarithms can be represented exactly, so the result of dividing them is not exact. The result you get is close to, but slightly less than 3. This gets rounded to 3 when being formatted by echo. floor, however returns 2.
You can avoid the inexact division by taking advantage of the fact that log(x, b) / log(y, b) is equivalent to log(x, y) (for any base b). This gives you the the expression log(238328, 62) instead, which has a floating point result of exactly 3 (the correct result since 238328 is pow(62, 3)).
It's due to the way floating point numbers are polished in PHP.
See the PHP Manual's Floating Point Numbers entry for more info
A workaround is to floor(round($value, 15));. Doing this will ensure that your number is polished quite accurately.
If you var_dump you'll see that the "3" is actually a float. Which means its probably close to 3 and rounded up. If you wanted 3, you would have to use the sister function, ceil.
You might get better results using the round() function and/or explicitly casting it to an int rather than relying on ceil(). Look here for more information: http://php.net/manual/en/language.types.integer.php
At the cost of a little performance, you could coerce it, reducing the precision to a more useful range by rounding or string formatting the number:
echo floor(round(log10(238328)/log10(62), 4));
echo floor(sprintf('%.4f', log10(238328)/log10(62)));
// output:
// 3
// 3
You should go with the minimum precision that you need. More precision is not what you want. Rounding without flooring might be more correct, the results are different depending on precision.
echo floor(round(log10(238328)/log10(62), 16));
echo round(log10(238328)/log10(62), 16);
// output:
// 2
// 3
there three functions for doing nearly the same:
ceil --> ceil(0.2)==1 && ceil(0.8)==1
floor --> floor(0.2)==0 && floor(0.8)==0
round --> round(0.2)==0 && round(0.8)==1