如何舍入到非零阈值

时间:2019-06-28 08:18:52

标签: php

我需要将给定价格四舍五入到非零阈值,例如9、8、3或5。

我尝试了series.tooltip.getFillFromObject = false; series.tooltip.background.fill = "blue"; series.adapter.add("tooltipText", function(tooltipText) { if (series.tooltipDataItem.dataContext.visits < 0) { series.tooltip.background.fill = "red"; } else { series.tooltip.background.fill = "blue"; } return tooltipText; }); round()floor()方法,但没有获得所需的结果。

ceil()

四舍五入至9欧元的示例:
15,00 €将是 19
17,25 €将是 19
1456,23 €将是 1459,00
19,01 €将是 29,00

1 个答案:

答案 0 :(得分:3)

您将“四舍五入”数字和10之间的差加到价格上(所以1等于9,2等于8,...),然后除以10。 ceil将值四舍五入到下一个整数,再乘以10,然后再次减去10与“四舍五入”数字之间的差:

$prices = [15, 17.25, 1456.23, 19.01];
$round_to = [9, 8, 3, 5];

foreach($round_to as $r) {
  foreach($prices as $price) {
    $rounded = ceil ( ( $price + 10 - $r ) / 10 ) * 10 - ( 10 - $r );
    echo $price . ' rounded up to next ' . $r . ' is '. $rounded . "\n";  
  }
  echo "\n";
}

输出:

15 rounded up to next 9 is 19
17.25 rounded up to next 9 is 19
1456.23 rounded up to next 9 is 1459
19.01 rounded up to next 9 is 29

15 rounded up to next 8 is 18
17.25 rounded up to next 8 is 18
1456.23 rounded up to next 8 is 1458
19.01 rounded up to next 8 is 28

15 rounded up to next 3 is 23
17.25 rounded up to next 3 is 23
1456.23 rounded up to next 3 is 1463
19.01 rounded up to next 3 is 23

15 rounded up to next 5 is 15
17.25 rounded up to next 5 is 25
1456.23 rounded up to next 5 is 1465
19.01 rounded up to next 5 is 25