php Rounding up numbers (3628 > 3630) [duplicate] - php

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

Related

How to set a decimal value up to 8 decimal places in php [duplicate]

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);

Make decimal value decrease or increase [duplicate]

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;

Finding the Prime Number using a WHILE LOOP (not a for loop)in php [duplicate]

This question already has answers here:
A formula to find prime numbers in a loop
(22 answers)
Closed 6 years ago.
So I am just starting to learn a PHP in college. I have been given a simple task of finding the prime numbers of 101 in PHP but using a while loop. I cant figure it out.It ahs to use a while loop rather a for loop, teacher specifications. Many thanks.
This is my pathetic code,(& this only prints out the numbers from 1 to 101, im ever new to it)
<?php
$x = 1;
$maxNumber=101;
while($x <= $maxNumber) {
echo "The number is: $x <br>";
$x++;
}
?>
So, you would devide by 2. Check if the number turns into a decimal. If so it's prime.
You can use is_float. Also 2/2=1. Ignore 1.

PHP return 1 number after dot in float [duplicate]

This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 6 years ago.
I have got this php code
$Likes=1112;
$Likes=$Likes/1000;
echo $Likes."k";
This code returns me 1.112k,but my goal is to get 1.1k
Use the number_format function:
echo number_format($Likes,1)."k";
And a coding style advice: don't start your variables with an upper case letter!

Computing limits in PHP [duplicate]

This question already has answers here:
Is there something that's larger than any numbers in PHP?
(9 answers)
Closed 8 years ago.
I want to compute n^(2/3) as n approaches infinity in PHP.
What I tried:
$n = "INF";
echo pow($n, (2/3));
Desired result:
INF
Actual result:
0
Any suggestions? How to compute limits in PHP?
UPDATE:
OK, first problem solved - I just needed to remove the quotation mark.
But is this actually the way to calculate a limit? Is it interpreted as a limit?
You're passing in the string "INF" into the exponential expression, which isn't valid. Try passing in just POW:
$n = INF;
echo pow($n, (2/3));
// INF

Categories