舍入机制至最接近的 0.05

发布于 2024-08-08 11:32:47 字数 593 浏览 4 评论 0原文

我想通过使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

那片花海 2024-08-15 11:32:47

使用这个功能

function rndfunc($x){
  return round($x * 2, 1) / 2;
}

use this function

function rndfunc($x){
  return round($x * 2, 1) / 2;
}
谎言 2024-08-15 11:32:47

从概念上讲,该过程可以如下完成:

  1. 除以 0.05
    • 或乘以 (1 / 0.05)
  2. 四舍五入到最接近的整数
  3. 乘以 0.05

Conceptually, the procedure can be done as:

  1. Divide by 0.05
    • or multiply by (1 / 0.05)
  2. Round to nearest integer
  3. Multiply by 0.05
酷遇一生 2024-08-15 11:32:47

您基本上希望将值映射到网格。网格定义为 0.05 的倍数。一般来说,您需要找到您的值所在的被乘数。

表中是负数。您需要决定是远离零(对称)舍入还是始终沿相同方向(即正)舍入。

代码:

$step = .05;
$multiplicand = floor( $value / $step );
$rest = $value % $step ;
if( $rest > $step/2 ) $multiplicand++; // round up if needed
$roundedvalue = $step*$multiplicand;

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:

$step = .05;
$multiplicand = floor( $value / $step );
$rest = $value % $step ;
if( $rest > $step/2 ) $multiplicand++; // round up if needed
$roundedvalue = $step*$multiplicand;
怪我鬧 2024-08-15 11:32:47

乘以二,然后四舍五入,然后除以二。

Multiply by two, then round, then divide by two.

菩提树下叶撕阳。 2024-08-15 11:32:47

提示:-

$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

掩耳倾听 2024-08-15 11:32:47
function round5Sen ($value) { 

    return number_format(round($value*20,0)/20,2,'.','');
} 

echo round5Sen(155.13);
echo "\n";
echo round5Sen(155.12);
echo "\n";
echo round5Sen(155.0);
echo "\n";
echo round5Sen(155.18);
echo "\n";
function round5Sen ($value) { 

    return number_format(round($value*20,0)/20,2,'.','');
} 

echo round5Sen(155.13);
echo "\n";
echo round5Sen(155.12);
echo "\n";
echo round5Sen(155.0);
echo "\n";
echo round5Sen(155.18);
echo "\n";
衣神在巴黎 2024-08-15 11:32:47

我确信有更优雅的解决方案,但这似乎适合这项任务:

<?php

// setup test
$start_num = 89.90;
$iterations = 10;

// loop through test numbers
for ($i = 0; $i < $iterations; $i++) {
  nickleRound($start_num + (0.01 * $i));
  echo "\n\n";
}

//
function nickleRound($num) {
  $p = 0.05;
  echo "\n" . 'p= ' . $p;

  $num = round($num, 2);
  echo "\n" . 'num= ' . $num;

  $r = ($num / $p);
  echo "\n" . 'r= ' . $r;

  $r2 = ceil($r) - $r;  
  echo "\n" . 'r2= ' . $r2;

  $a = round($num, 1);
  if (($r2 > 0) && ($r2 < 0.5)) {
    $a = $a + 0.05; 
  }
  echo "\n" . 'a= ' . $a;
}

I'm sure there are more elegant solutions, but this appears to suit the task:

<?php

// setup test
$start_num = 89.90;
$iterations = 10;

// loop through test numbers
for ($i = 0; $i < $iterations; $i++) {
  nickleRound($start_num + (0.01 * $i));
  echo "\n\n";
}

//
function nickleRound($num) {
  $p = 0.05;
  echo "\n" . 'p= ' . $p;

  $num = round($num, 2);
  echo "\n" . 'num= ' . $num;

  $r = ($num / $p);
  echo "\n" . 'r= ' . $r;

  $r2 = ceil($r) - $r;  
  echo "\n" . 'r2= ' . $r2;

  $a = round($num, 1);
  if (($r2 > 0) && ($r2 < 0.5)) {
    $a = $a + 0.05; 
  }
  echo "\n" . 'a= ' . $a;
}
↘紸啶 2024-08-15 11:32:47

对 @xtofl 进行一些扩展以允许更精确的步骤(从技术上讲,这个问题不需要)

    $step         = 0.0005;
    $multiplicand = floor($value / $step);
    $rest         = fmod($value, $step);

    $value = $step * $multiplicand;

    if ($rest > $step / 2) {
        $value += $step;
    }

Expanding a little on @xtofl to allow for more precise steps (not technically required for this question)

    $step         = 0.0005;
    $multiplicand = floor($value / $step);
    $rest         = fmod($value, $step);

    $value = $step * $multiplicand;

    if ($rest > $step / 2) {
        $value += $step;
    }
爱冒险 2024-08-15 11:32:47
//Round to nearest 0.05
echo round ($number * 20, 0) / 20;

//Round Up to nearest 0.05
echo ceil ($number * 20) / 20;

//Round Down to nearest 0.05
echo floor ($number * 20) / 20;
//Round to nearest 0.05
echo round ($number * 20, 0) / 20;

//Round Up to nearest 0.05
echo ceil ($number * 20) / 20;

//Round Down to nearest 0.05
echo floor ($number * 20) / 20;
九歌凝 2024-08-15 11:32:47

感谢 @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

不必在意 2024-08-15 11:32:47

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文