Rounding Mechanism to nearest 0.05

后端 未结 10 2022
离开以前
离开以前 2021-01-05 00:15

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/in

10条回答
  •  南笙
    南笙 (楼主)
    2021-01-05 00:22

    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;
    

提交回复
热议问题