0
How to find the next number that is multiple of 10 in PHP?
Example:
$a = 142;
How to get number 150?
0
How to find the next number that is multiple of 10 in PHP?
Example:
$a = 142;
How to get number 150?
2
Good afternoon.
Well I don’t know if there’s a specific function for that, but you can do it this way using the ceil
, and a simple mathematical logic.
Example:
$num = 142;
$arredondado = (ceil($num/10))*10;
//resultado 150;
echo $arredondado;
Simply divide the number by 10, and round it up to catch the next number, then just multiply it by 10 and you have the desired number.
Like I said, I don’t know if it has a specific function, but it works.
I hope it helps.
Att;
2
Basically this:
round( $a + 4, -1 );
Applied to your code:
$a = 142;
echo round( $a + 4, -1 );
See working on IDEONE.
Round serves for rounding of numbers in n
decimal places, but if you use negative numbers, you refer to the tens, hundreds etc.
The -1 is 10 in 10, -2 of 100 in 100, and so on.
The +4 serves to always round up (PHP has a constant to choose this behavior in decimals, but it doesn’t work right for dozens) - note that this only makes sense for integers. If it’s for use with decimals, the best solution from @Fleuquer
Browser other questions tagged php
You are not signed in. Login or sign up in order to post.
Thanks! Taken care of!
– Paulo Rogerio
Please put as an answer if you have solved your problem. Thank you
– Fleuquer Lima
Could remove unnecessary quotes, since it is a function
$arredondado = ceil($num/10)*10;
– Guilherme Nascimento