Hello I''ve encountered a little problem with zeros using the decimal data type Specifying a scale of 3 and inserting a number with 3 zeros after the point (for example 12345.000) it gets correctly stored in the database, but rails insists to show me the number only with the first zero after the point (12345.0) There is a way to tell rails to not truncate at the first zero?
On Mon, Aug 3, 2009 at 11:37 AM, lucac81<lcorsini-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > There is a way to tell rails to not truncate at the first zero?You can use sprintf to always force printing up to 3 decimal places, eg sprintf "%.3f", the_number
On 03 Aug 2009, at 12:20, Franz Strebel wrote:>> There is a way to tell rails to not truncate at the first zero? > > You can use sprintf to always force printing up to 3 decimal places, > eg > > sprintf "%.3f", the_numberOr even better: number_with_precision http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#M001687 Best regards Peter De Berdt --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Aug 3, 12:29 pm, Peter De Berdt <peter.de.be...-LPO8gxj9N8aZIoH1IeqzKA@public.gmane.org> wrote:> On 03 Aug 2009, at 12:20, Franz Strebel wrote: > > >> There is a way to tell rails to not truncate at the first zero? > > > You can use sprintf to always force printing up to 3 decimal places, > > eg > > > sprintf "%.3f", the_number > > Or even better: number_with_precision > > http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.ht... > > Best regards > > Peter De BerdtThat should do the trick, i''ll try later Thank you very much Luca
On Aug 3, 3:43 pm, lucac81 <lcors...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Aug 3, 12:29 pm, Peter De Berdt <peter.de.be...-LPO8gxj9N8aZIoH1IeqzKA@public.gmane.org> wrote: > > > > > On 03 Aug 2009, at 12:20, Franz Strebel wrote: > > > >> There is a way to tell rails to not truncate at the first zero? > > > > You can use sprintf to always force printing up to 3 decimal places, > > > eg > > > > sprintf "%.3f", the_number > > > Or even better: number_with_precision > > >http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.ht... > > > Best regards > > > Peter De Berdt > > That should do the trick, i''ll try later > > Thank you very muchUnfortunately this work only in the views, my program has to generate a text file, and in the file the number is still truncated I need to find another solution Luca
The short, quick way to do it in any context is with plain Ruby: "%.03f" % value --Matt Jones On Aug 4, 4:14 am, lucac81 <lcors...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Aug 3, 3:43 pm, lucac81 <lcors...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > On Aug 3, 12:29 pm, Peter De Berdt <peter.de.be...-LPO8gxj9N8aZIoH1IeqzKA@public.gmane.org> wrote: > > > > On 03 Aug 2009, at 12:20, Franz Strebel wrote: > > > > >> There is a way to tell rails to not truncate at the first zero? > > > > > You can use sprintf to always force printing up to 3 decimal places, > > > > eg > > > > > sprintf "%.3f", the_number > > > > Or even better: number_with_precision > > > >http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.ht... > > > > Best regards > > > > Peter De Berdt > > > That should do the trick, i''ll try later > > > Thank you very much > > Unfortunately this work only in the views, my program has to generate > a text file, and in the file the number is still truncated > I need to find another solution > Luca