on Wed, May 08, 2002 at 01:18:52PM +0100, Richard Kettlewell wrote:
Why not:
$a = int(100 * $a)/100.0;
Because that truncates the number; it doesn't round. Plus seems to be slower than sprintf(), especially with a high enough number of decimal places required. Implementing it as a generic "reduce to n d.p." would probably require one exponent to an unknown, and not base 2, so probably not terribly easy for a compiler or interpreter to optimize. (afaik)
Which will work, but personally I would try to avoid reducing the number of decimal places that are stored in the variable and instead do the reduction when the variable was printed, i.e. use sprintf and printf for printing the values.
Sure, and printf is synonymous to print sprintf(). (It would seem fairly logical for printf to feed the value of sprintf() into print() and not interpret the values itself.)