PHP:从数字中获取数千

时间:2011-09-02 12:09:33

标签: php

当用户输入1000以上的数字时,我希望能够在数组中获得该数字的数千。

例如......

用户输入的数字:165124

我的数组应该返回:

array('thousand_low' => 165000, 'thousand_high' = 165999)

谢谢!

6 个答案:

答案 0 :(得分:5)

完整的数组返回函数,使用PHP的原生floorceil函数:

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

答案 1 :(得分:2)

这样的事情:

$num = 165124;

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

答案 2 :(得分:2)

未经测试(编辑:但应该工作;)):

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

答案 3 :(得分:0)

查看圆函数(http://php.net/manual/en/function.round.php) - 您可以指定精度,以便自定义舍入的大小。

答案 4 :(得分:0)

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

答案 5 :(得分:0)

在相当长的一段时间内没有使用过php,但我认为应该看起来像这样:

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