2

I don't know how to title the question correctly (please tell me what this kind of number called).

I'd like to convert from 2 digits number into a 10 divisible number. For example, I expect:

  • 15 to become 10
  • 23 to become 20
  • 38 to become 30
  • 999 to become 900
  • 9999 to become 9000

I tried searching for the solution on google but I don't know how to type the proper word.

Wilf
  • 2,297
  • 5
  • 38
  • 82
  • You should convert it to a float by dividing by 10(00,ect..) then convert it to an int and multiply it again by the same number you use to divide – Tit-oOo Mar 30 '15 at 10:41

5 Answers5

3

Easy, use the PHP floor function: http://php.net/manual/en/function.floor.php

floor($number/10) * 10
Richard
  • 303
  • 3
  • 9
2

I wrote a simple function should work:

<?php
function roundDown($var){
    $len = strlen($var)-1;
    $divide = 1;
    for($i=1;$i<=$len;$i++){
        $divide .= 0;
    }


    return floor($var/$divide)*$divide;
}

echo roundDown(9999);
Daan
  • 12,099
  • 6
  • 34
  • 51
1

Easiest way is divide by ten, then floor value and multiply by ten. (Floor method - phpdocs)

floor($number/10)*10
Tomasz Ferfecki
  • 1,263
  • 14
  • 22
0

Use round:

echo round(1241757, -6); // 1000000

from http://php.net/manual/en/function.round.php

Lydia Ralph
  • 1,455
  • 1
  • 17
  • 33
0

You are looking for the "one significant figure". I think this answer provides a good solution:

How to round down to the nearest significant figure in php

Short form:

$x = $y - $y % pow(10, floor(log10($y)));
Community
  • 1
  • 1
Flosculus
  • 6,880
  • 3
  • 18
  • 42