Find out next multiple of 10 of a number in php

0

How to find the next number that is multiple of 10 in PHP?

Example:

$a = 142;

How to get the number 150?

    
asked by anonymous 18.04.2016 / 19:38

2 answers

2

Basically this:

round( $a + 4, -1 );

Applied to your code:

$a = 142;
echo round( $a + 4, -1 );

See working at IDEONE .

Round is used for rounding numbers in% with% decimal places, but if you use negative numbers, it refers to tens, hundreds, etc.

The -1 is 10 in 10, -2 in 100 in 100, and so on.

+4 serves to always round up (PHP has a constant to choose this behavior in decimals, but that does not work right for tens) - note that this only makes sense for integers. If it is for use with decimals, the @Fleuquer solution

    
18.04.2016 / 20:03
1

Good afternoon. Well I do not know if there is a specific function for this, but you can do it this way using ceil , and 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 get the next number, then simply multiply it by 10 and get the desired number.

As I said, I do not know has a specific function, but it works.

I hope it helps.

Att;

    
18.04.2016 / 19:51