This question already has answers here:
How to round up a number to nearest 10?
(16 answers)
Closed 4 years ago.
I'm probably not looking hard enough but the common question about php rounding is rounding up, not down.
For example I am trying round this..
<?php $roundDown = 768; ?>
Down to..
<?php var_dump($roundDown) /* 700 */ ?>
Whats the simplest method to do this, or is it because the number is closer to 800 that it's not technically rounding?
Whats the function that I need to do this if it's not rounding?
A little point in the right direction would be much appreciated.
You can try ceil() and floor() function.
echo floor(768 / 100) * 100; // Output:700
echo ceil(768 / 100) * 100; // Output:800
Related
This question already has answers here:
Delete digits after two decimal points, without rounding the value
(15 answers)
Closed 3 years ago.
I'm trying to show one decimal place for rating number I have
but it returns unexpected value.
I used number_format and the round functions and both have the same issue, or i'm doing something wrong.
I tried to make this number show one decimal number
4.96 and it always returns 5 instead of 4.9
number_format(4.96, 1)
round(4.96, 1)
round(4.96, 1,PHP_ROUND_HALF_DOWN)
both functions returns 5 instead of 4.9
I searched all answers but couldn't find anything helpful.
Rounding 4.96 will round .9 up, so it will be 5 in all cases. If you want to do it without rounding, you may have to tweak it a bit to fool it:
floor(4.96 * 10) / 10; // 4.9
Here's a function you can use to achieve this.
function convertToSingleDecimal($num, $precision = 2) {
return floor($num) . substr(str_replace(floor($num), '', $num), 0, $precision + 1);
}
print convertToSingleDecimal("4.96", 1);
This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 3 years ago.
the result of this should be zero!
echo array_sum([-61.50,50.00,10.50,1.00,0.00,50.00,-16.73,-20.00,-55.75,42.48]);
Why is giving -7.105427357601E-15?
Just try round(), you will get the same result.
echo round(array_sum([-61.50,50.00,10.50,1.00,0.00,50.00,-16.73,-20.00,-55.75,42.48]));
Because floating point values (which you have here when you use decimals) are not exact. They're approximations.
The error in that approximation comes out to -7.105427357601E-15 when summing these values.
It's because of floats. If you want to calculate something with precision 2 (for this example) you should use something like this:
$el = [-61.50,50.00,10.50,1.00,0.00,50.00,-16.73,-20.00,-55.75,42.48];
$sum = 0;
foreach ($el as $e) {
$sum += $e * 100;
}
echo $sum / 100;
You should never trust to float values. Another example from Javascript (Google developer Console):
This question already has answers here:
Display float value w/o scientific notation
(4 answers)
Closed 5 years ago.
I am working on my site about bitcoins. And people can have balances there. I am btw working in php. In my $user_data["balance"] the value is 50. And I want that to get converted by php into 0.00000050. So I tried this:
$balance = $user_data["balance"] / 1000000;
But what I get from it, is this: 5.0E-5. How to change it into 0.00000050?
I would be really thankfull if somebody would help me!
Use number_format to get results with 8 decimals.
Example-
$balance = number_format($user_data["balance"] / 1000000,8,".","");
echo $balance; //output will 0.00005000
But if you want 0.00000050 then divide it by 100000000
Example
$balance = number_format($user_data["balance"] / 100000000,8,".","");
echo $balance; //output will 0.00000050
This question already has answers here:
Round to max thousand, hundred etc in PHP
(5 answers)
Closed 5 years ago.
I have a questions to decrease or increase decimal value in my PHP. For example I have 2573000.00 I want to make it 2500000.00. I have tried to use round() but not give me best answer. How to do that ?
Thank you.
Try this:
$a = 2573000.00;
$b = (int)($a / 100000) * 100000;
echo $b;
This question already has answers here:
How to round up a number to nearest 10?
(16 answers)
Closed 6 years ago.
After converting currencies I end up with the numbers below:
3628, 5987 and 2359.
I'd like to round them up so they would appear as 3630, 5990 and 2360.
What is the best approach to accomplish this?
My idea of doing this is adding a 0. in front of the number to achieve 0.3628, then using round(0.3628, 3) so that I would get 0.363 and then finally id have to remove 0. and add another zero at the end to achieve 0.3630.
There must be a better way to do it.
Like so:
echo ceil(5987 / 10) * 10;
outputs 5990