Don't trust sprintf(). For whatever whim people had, it has some very arbitrary ideas about how to round numbers. On some platforms, it rounds 0.5 down, but 1.5 up. I did a test on Solaris and Windows, and the results varied. (Both versions just rely on their C runtime implementations of sprintf().)
If you care about numbers, do the math yourself. Learn how floating point numbers on computers work, and where they're likely to introduce problems. For example, you're forcing a division before a multiplication in your loop. This reduces precision in floating point numbers. It's better to say $percent = ($x * 100 / $y);.
Once you've got the floating value, use $rounded = int($unrounded + ($unrounded <=> 0)*0.5);. (This uses the spaceship operator like a sgn() function.)
--
[ e d @ h a l l e y . c c ]
In reply to Re: How to get rounded numbers to =100% ??
by halley
in thread How to get rounded numbers to =100% ??
by alarix
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |