开发者

PHP: Get thousand from number

When the user enters a number from 1000 and above I want to be able to get the thousand for that number in an array.

For example…

Number entered by user: 165124

My array should return:

array('thousand_low' => 165000, 'thousand_开发者_开发知识库high' = 165999)

Thanks!


The complete array-returning function, using PHP's native floor and ceil functions:

function get_thousands($num) {
  return array(
    'thousand_low'=>floor($num/1000)*1000,
    'thousand_high'=>ceil($num/1000)*1000-1
  );
}


Untested (edit: but should work ;) ):

$number = 165124;
$low    = floor($number / 1000) * 1000;
$high   = $low + 999;


Something like this:

$num = 165124;

$result = array();
$result['thousand_low'] = floor($num / 1000) * 1000;
$result['thousand_high'] = $result['thousand_low'] + 999;


Have a look at the round function (http://php.net/manual/en/function.round.php) - you can specify the precision so you can customise the magnitude of the rounding.


 array('thousand_low' => floor($int/1000)*1000, 
'thousand_high' => floor($int/1000)*1000+999);


Haven't used php in quite a while but i think it should look something like this :

$num_input = 165124;
$array['thousand_low'] = floor($num_input /  1000) * 1000;
$array['thousand_high'] = $array['thousand_low'] + 999;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜