Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding Mechanism to nearest 0.05

Tags:

php

rounding

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.

like image 920
Shiro Avatar asked Oct 20 '09 03:10

Shiro


2 Answers

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
like image 77
Craig McQueen Avatar answered Sep 20 '22 23:09

Craig McQueen


use this function

function rndfunc($x){
  return round($x * 2, 1) / 2;
}
like image 36
mauris Avatar answered Sep 19 '22 23:09

mauris