Hi, I have a float that often looks like somthing like 2003.2 in my controller (1 decimal place) Is there a way to always show it to 2 decimal places eg 2003.20 ? Thanks Scott -- Posted via ruby-forum.com.
Ruby uses"printf" format conventions. You can use: printf ("%.2f",2003.2) ==> 2003.20 _Hari -- View this message in context: nabble.com/always-show-2-decimal-places-tf1872577.html#a5118477 Sent from the RubyOnRails Users forum at Nabble.com.
Nara Hari wrote:> Ruby uses"printf" format conventions. > > You can use: > > printf ("%.2f",2003.2) ==> 2003.20I need to be able to store this in a nother varibale in a controller, is this possible? -- Posted via ruby-forum.com.
sprintf("%.2f", number) just an idea: class Float def to_s(digits) sprintf("%.#{digits}f", self) end end 12.0.to_s(2) > 12.00 ss schrieb:> Hi, > > I have a float that often looks like somthing like 2003.2 in my > controller (1 decimal place) > > Is there a way to always show it to 2 decimal places eg 2003.20 ? > > Thanks > Scott > >
Just FYI: If you see only one decimal place inside of a form field, you might want to look at the precision (number of decimal places) in your database. For example, MySQL defaults to 1 for floats. As soon as you change the precision to 2, you''ll see 2 places. This is, of course, only when using <%=text_field %> and similar. On 6/30/06, Pete <pertl@gmx.org> wrote:> > sprintf("%.2f", number) > > just an idea: > > class Float > def to_s(digits) > sprintf("%.#{digits}f", self) > end > end > > 12.0.to_s(2) > > > 12.00 > > > > ss schrieb: > > Hi, > > > > I have a float that often looks like somthing like 2003.2 in my > > controller (1 decimal place) > > > > Is there a way to always show it to 2 decimal places eg 2003.20 ? > > > > Thanks > > Scott > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: wrath.rubyonrails.org/pipermail/rails/attachments/20060630/9037466b/attachment.html