This question already has answers here:
Rounding to nearest fraction (half, quarter, etc.)
(4 answers)
Closed 3 years ago.
I was wondering what php function could help me to do what I am looking for. I want to round number to the half up.
For example:
1.16 => 1.5,
0.5 => 0.5
1 => 1
I wanted to use round function like this :
round(1.16, 0.5, PHP_ROUND_HALF_UP)
But it returns 1 instead of 1.5. I probably dont understand well what the function is doing.
Here is one option. We can multiply the input number by 2, then take the ceiling, and finally divide by two, e.g.
$input = 1.16;
$input_rounded = ceil(2*$input) / 2;
echo $input_rounded;
Related
This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 3 years ago.
I want to show amount of bitcoins up to 8 decimal points. I have stored the value in Satoshi but when I try to convert the satoshi to BTC by dividing the satoshi value with 100000000 I am getting invalid result. here is my code.
$res = 3535/100000000;
echo $res;
the output of the above code is 3.535E-5 isntead 0.00003535.
please tell me the proper solution.
echo number_format(3535/100000000, 8);
Where 8 is the count of 0's. For example for 3535/1000000000 you need to write
echo number_format(3535.0/1000000000, 9);
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:
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
This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 9 years ago.
how can i shorten a long number with php ? So a number like 0.3483748937847832 would become 0.34
Please search stackoverflow first - from PHP: show a number to 2 decimal places
number_format()
return number_format((float)$number, 2, '.', '');
https://stackoverflow.com/a/4483561/689579
or
$padded = sprintf('%0.2f', $unpadded); // 520 -> 520.00
https://stackoverflow.com/a/4483715/689579
Use round
echo round($number,2);