how can I get a float to always print two decimal places? Thanks, Jamie
maybe with sprintf("%.2f", 0.123456) => "0.12" -- johan On Sat, 29 Jan 2005 18:53:46 -0500, Jamie Orchard-Hays <jamie-fswG1Ka7Iew@public.gmane.org> wrote:> how can I get a float to always print two decimal places? > > Thanks, > Jamie > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Johan Sørensen Professional Futurist www.johansorensen.com
Thanks, that did it. Is this the preferred syntax? <%= sprintf("%.2f",@item.distance) %> Jamie On Jan 29, 2005, at 6:58 PM, Johan Sörensen wrote:> sprintf("%.2f", 0.123456)
You can also use String#% (i.e. the modulo operator), which gives the same result: <%= "%.2f" % @item.distance %> Looks cleaner (imho) when there''s only one value involved, but multiple values have to be put in an array, which looks a bit messy. I tend to use String#% when there''s only one value involved and sprintf for multiple values. //samuel 2005-01-30 kl. 01.02 skrev Jamie Orchard-Hays:> Thanks, that did it. Is this the preferred syntax? > > <%= sprintf("%.2f",@item.distance) %> > > Jamie > > On Jan 29, 2005, at 6:58 PM, Johan Sörensen wrote: > >> sprintf("%.2f", 0.123456) > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >________________________ Samuel Kvarnbrink blog: http://samuelk.info mail: samuel-Z3oqqNSG8ADLoDKTGw+V6w@public.gmane.org samuel.kvarnbrink-aIfHlyXHbjfLxA5HGQDqww@public.gmane.org samuel.kvarnbrink-SiIintg/mUyhy6ZWzZ/+Cw@public.gmane.org "On two occasions I have been asked [by members of Parliament!], Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?'' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question." -- Charles Babbage
Thanks for that! Jamie On Jan 30, 2005, at 2:04 PM, Samuel Kvarnbrink wrote:> You can also use String#% (i.e. the modulo operator), which gives the > same result: > > <%= "%.2f" % @item.distance %> > > Looks cleaner (imho) when there''s only one value involved, but > multiple values have to be put in an array, which looks a bit messy. I > tend to use String#% when there''s only one value involved and sprintf > for multiple values. > > //samuel > > 2005-01-30 kl. 01.02 skrev Jamie Orchard-Hays: > >> Thanks, that did it. Is this the preferred syntax? >> >> <%= sprintf("%.2f",@item.distance) %> >> >> Jamie >> >> On Jan 29, 2005, at 6:58 PM, Johan Sörensen wrote: >> >>> sprintf("%.2f", 0.123456) >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > > ________________________ > Samuel Kvarnbrink > > blog: http://samuelk.info > > mail: samuel-Z3oqqNSG8ADLoDKTGw+V6w@public.gmane.org > samuel.kvarnbrink-aIfHlyXHbjfLxA5HGQDqww@public.gmane.org > samuel.kvarnbrink-SiIintg/mUyhy6ZWzZ/+Cw@public.gmane.org > > > "On two occasions I have been asked [by members of Parliament!], > Pray, Mr. Babbage, if you put into the machine wrong figures, > will the right answers come out?'' I am not able rightly to > apprehend the kind of confusion of ideas that could provoke such > a question." > -- Charles Babbage > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >