From: Ara.T.Howard@... Date: 2004-10-16T03:34:36+09:00 Subject: Re: Rounding to X digits --8323328-190858547-1097864601=:30612 Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-190858547-1097864601=:30612" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --8323328-190858547-1097864601=:30612 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE On Sat, 16 Oct 2004, trans. (T. Onoma) wrote: > On Friday 15 October 2004 11:14 am, Ara.T.Howard@noaa.gov wrote:| =C2=A0 = =C2=A0jib:~/eg/ruby > cat truncate.rb| =C2=A0 =C2=A0class Float| =C2=A0 =C2= =A0 =C2=A0def truncate sd =3D 2| =C2=A0 =C2=A0 =C2=A0 =C2=A0i =3D self.to_i= | =C2=A0 =C2=A0 =C2=A0 =C2=A0prec =3D 10 ** sd| =C2=A0 =C2=A0 =C2=A0 =C2=A0= fraction =3D (i.zero? ? self : (self / i) - 1)| =C2=A0 =C2=A0 =C2=A0 =C2=A0= i + (fraction * prec).to_i / prec.to_f| =C2=A0 =C2=A0 =C2=A0end| =C2=A0 =C2= =A0end|| =C2=A0 =C2=A0p(0.1 * (134.45/0.1).truncate) =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 #=3D> 134.4| =C2=A0 =C2=A0p((134.= 45 * (1/0.1)).truncate.to_f / (1/0.1)) =C2=A0 =C2=A0#=3D> 134.4|| =C2=A0 = =C2=A0jib:~/eg/ruby > ruby truncate.rb| =C2=A0 =C2=A0134.4| =C2=A0 =C2=A013= 4.4 > Sorry, I don't follow. Isn't that answer incorrect? The mid-point should = round up, not down. At least, that's what I was taught in school. > T. sorry- not only did i post the wrong version, but mine clobbered the existing truncate in Float, this ought to work: jib:~/eg/ruby > cat roundn.rb class Float def roundn sd =3D 2 f =3D self.floor prec =3D 10.0 ** sd.to_i.abs frac =3D (f =3D=3D 0 ? self : self - f) f + (frac * prec).round / prec end end p(0.1 * (134.45/0.1).roundn) #=3D> 134.45 p((134.45 * (1/0.1)).roundn.to_f / (1/0.1)) #=3D> 134.45 jib:~/eg/ruby > ruby roundn.rb 134.45 134.45 regards. -a ps. i'm cc'ing you directly to let you know your message ends up as one bi= g line in my mailer and i'm wondering if it's on my end or yours? -- =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | When you do something, you should burn yourself completely, like a good | bonfire, leaving no trace of yourself. --Shunryu Suzuki =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D --8323328-190858547-1097864601=:30612-- --8323328-190858547-1097864601=:30612--