php - PHP 中的舍入时间小数点后
问题描述
$time_taken = 286;
$time_taken = $time_taken / 60; //Converting to minutes
echo $time_taken;
结果:4.7666666666667
但我需要:(期望:)
结果:5.17(预期)
我试过:round($time_taken,2);
但后来它给出了结果:
结果:4.77
解决方案
你读错了结果。但别担心。与时间打交道曾经让大多数开发者发疯。这就像一个通过仪式。
你得到了4.76 minutes
,这是不一样的4 minutes and 76 seconds
。
是4 full minutes and 0.76 of a minute
。
分解它:
4 minutes = 240 sec
286 - 240 = 46
所以结果应该是 4 分 46 秒。
要计算它,您可以这样做:
$total = 286;
// Floor the minutes so we only get full minutes
$mins = floor($total / 60);
// Calculate how many secs are left
$secs = $total % 60; // Thanks @RiggsFolly for the tip
echo "$mins minutes and $secs seconds";
推荐阅读
- html - 我的标题在按钮悬停效果时晃动
- javascript - JavaScript 动态同步函数队列
- sql-server - 在 If 语句中使用值后从存储过程中返回一个值
- r - 文件中出现错误(文件,“rt”):无法打开连接
- monitoring - Geneos 文件系统监控 .snapshot
- javascript - 带有 Typescript TS2604 错误的动态导入 ES6 React 组件?
- entity - 是否可以在 Visio 中使用类似 XL 表的结构创建 ERD?我需要在每个属性旁边显示列(单选框)
- php - 来自 MySQL 数据库的数据显示在文本字段中,但不通过 php 显示在文本区域中
- c++ - 浮点异常(核心转储)#694457
- html - 关于影子菜单问题的问题