Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
716 views
in Technique[技术] by (71.8m points)

php - Rounding Mechanism to nearest 0.05

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

use this function

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...