舍入机制至最接近的 0.05
我想通过使用 php4,5.2 及以下版本(不是 5.3)来解决舍入机制 目前我正在进行 0.05 舍入,类似于此页面:
http://www.bnm.gov.my/index.php?ch=209&pg=657&ac=568
before rounding | after rounding
89.90 | 89.90
89.91 | 89.90
89.92 | 89.90
89.93 | 89.95
89.94 | 89.95
89.95 | 89.95
89.96 | 89.95
89.97 | 89.95
89.98 | 90.00
89.99 | 90.00
我尝试使用字符串将其拆分并手动添加,但并不是真正的很好的解决方案,希望能在这里找到人来解决。
I would like to solve rounding mechanism by using php4,5.2 and below (not 5.3)
Currently I am doing 0.05 rounding, something like this page:
http://www.bnm.gov.my/index.php?ch=209&pg=657&ac=568
before rounding | after rounding
89.90 | 89.90
89.91 | 89.90
89.92 | 89.90
89.93 | 89.95
89.94 | 89.95
89.95 | 89.95
89.96 | 89.95
89.97 | 89.95
89.98 | 90.00
89.99 | 90.00
I try to use string to split it out and manually do adding, but not really a good solution, hoping here can find someone to solve it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
使用这个功能
use this function
从概念上讲,该过程可以如下完成:
Conceptually, the procedure can be done as:
您基本上希望将值映射到网格。网格定义为
0.05 的倍数
。一般来说,您需要找到您的值所在的被乘数。表中不是负数。您需要决定是远离零(对称)舍入还是始终沿相同方向(即正)舍入。
代码:
You basically want to map values to a grid. The grid is defined as
a multiple of .05
. In general, you need to find the multiplicands your value lies between.What isn't in the table are the negative numbers. You need to decide on whether to round away from zero (symmetrical) or always in the same direction (i.e. positive).
code:
乘以二,然后四舍五入,然后除以二。
Multiply by two, then round, then divide by two.
提示:-
$input1 = 24.05;
$东西 = 绝对($输入 * 20 ); 481 章
第 // 48 ".05"s
$ouput = $tenpcnt / 20;
回显$输出; // 2.40
Hint:-
$input1 = 24.05;
$things = abs($input * 20 ); // 481 ".05"s
$tenpcnt = abs($things / 10); // 48 ".05"s
$ouput = $tenpcnt / 20;
echo $ouput; // 2.40
我确信有更优雅的解决方案,但这似乎适合这项任务:
I'm sure there are more elegant solutions, but this appears to suit the task:
对 @xtofl 进行一些扩展以允许更精确的步骤(从技术上讲,这个问题不需要)
Expanding a little on @xtofl to allow for more precise steps (not technically required for this question)
感谢 @mauris 提供的解决方案解决了我有关马来西亚 GST 舍入机制的问题。它也适用于 SQL。
声明 @tempTable AS TABLE(Number Decimal(20,4));
插入@tempTable值(89.90),(89.91),(89.92),(89.93),(89.94),(89.95),(89.96),(89.97),(89.98),(89.99)
选择数字,轮(数字* 2, 1) / 2 AS 来自 @tempTable 的“四舍五入”
Thank you @mauris for the solution to solve my problem on Malaysia GST rounding mechanism. It also works in SQL.
DECLARE @tempTable AS TABLE(Number Decimal(20,4));
INSERT INTO @tempTable VALUES (89.90),(89.91),(89.92),(89.93),(89.94),(89.95),(89.96),(89.97),(89.98),(89.99)
SELECT Number, round(Number * 2, 1) / 2 AS 'Rounded' FROM @tempTable
PHP 具有适用于 PHP 4、PHP 5、PHP 7、PHP 8 的函数 round()
https://www.php.net/manual/en/function.round.php
PHP has the function round() for the PHP 4, PHP 5, PHP 7, PHP 8
https://www.php.net/manual/en/function.round.php