On Sat, 16 Oct 2004, GOTO Kentaro wrote: > In message Re: Rounding to X digits > on Thu, 14 Oct 2004 23:49:31 +0900, wrote "Robert Klemme": > > The difference in efficiency is one issue. But there is a conceptual > > difference: it depends whether you want to align printed output or round a > > numeric value in a calculation. For the first task sprintf is probably > > better because the amount of places printed is guaranteed (by the format > > string), while it's not if you just round the number and print it. > > And there is an algorithmic difference: many implementation of > sprintf(3) conform to ISO 31-0 but Float#round doesn't. > > puts 0.5.round #=> 1 > puts sprintf("%.0f", 0.5) #=> 0 > Both are (IIRC) ISO 31-0 conformant, but one uses ISO 31-0 B.3 rule A and the other uses ISO 31-0 B.3 rule B. That said, one of the rules (the one sprintf uses, to round down following even digits is an abomination and should be eliminated from the face of the earth. nnn.nn5 should ALWAYS round up and rounded values should not be re-rounded (which is what the contrary rule assumes is happening). -- Markus