【问题标题】:Float return 0 when the value is under 1当值小于 1 时浮点数返回 0
【发布时间】:2017-04-04 13:43:42
【问题描述】:

我有问题。我用 echo 函数打印一个值不会打印结果,而是用 var_dump 返回值。

//$issue['fields']['timeestimate'] value is 2700
echo $this->toHours($issue['fields']['timeestimate']); return nothing
var_dump($this->toHours($issue['fields']['timeestimate'])); return float(0.75)

功能是

private function toHours($seconds){
        return $seconds/3600;
}

怎么了?如果值为 1 或更高,则返回正确的值。

【问题讨论】:

  • 试试php.net/manual/en/function.ceil.php 我想这就是你所追求的......
  • 很可能是精度问题。阅读大红框php.net/manual/en/language.types.float.php
  • 不起作用。 echo 函数继续不返回任何内容。
  • 这里的代码没问题,bug肯定在别的地方。编码? timeestimate 中的空格或特殊字符。请显示所有var_dump($issue['fields']['timeestimate']) var_dump($seconds/3600) var_dump($this->toHours($issue['fields']['timeestimate'])); 真实(复制和粘贴)

标签: php


【解决方案1】:

也许你可以在除法之前转换为数值:

private function toHours($seconds){
        $seconds = intval($seconds);
        return $seconds/3600;
}

并使用 printf() 显示格式化的浮点值,如:

printf('%f', $this->toHours($issue['fields']['timeestimate']));

【讨论】:

    【解决方案2】:

    错误在处理它的循环中,对不起...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      • 1970-01-01
      • 2020-03-07
      • 2019-03-16
      • 2019-04-30
      • 1970-01-01
      • 2022-01-14
      相关资源
      最近更新 更多