Hi - I am working with floats and am wondering how I can trim the decimal and trailing zeros on my whole numbers. For instance, whole numbers are being displayed as 1.0, 2.0, 3.0, etc... I would like to remove the decimal and any trailing zeros with out effecting other non-whole number floats (ex. 1.375). Any ideas? Help appreciated. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Maybe you can convert them to integers? my_float = 2.0 my_float = my_float.to_i if my_float == my_float.to_i Pepe On Feb 27, 1:20 am, northband <northb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi - > > I am working with floats and am wondering how I can trim the decimal > and trailing zeros on my whole numbers. For instance, whole numbers > are being displayed as 1.0, 2.0, 3.0, etc... I would like to remove > the decimal and any trailing zeros with out effecting other non-whole > number floats (ex. 1.375). > > Any ideas? Help appreciated.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks - this seems easy enough. On Feb 27, 3:12 pm, pepe <P...-1PhG29ZdMB/g+20BJ0uB2w@public.gmane.org> wrote:> Maybe you can convert them to integers? > > my_float = 2.0 > my_float = my_float.to_i if my_float == my_float.to_i > > Pepe > > On Feb 27, 1:20 am, northband <northb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi - > > > I am working with floats and am wondering how I can trim the decimal > > and trailing zeros on my whole numbers. For instance, whole numbers > > are being displayed as 1.0, 2.0, 3.0, etc... I would like to remove > > the decimal and any trailing zeros with out effecting other non-whole > > number floats (ex. 1.375). > > > Any ideas? Help appreciated.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
The %g specifier to sprintf will do this automatically: "%g" % 1.0 => "1" "%g" % 1.375 => "1.375" --Matt Jones On Feb 27, 1:20 am, northband <northb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi - > > I am working with floats and am wondering how I can trim the decimal > and trailing zeros on my whole numbers. For instance, whole numbers > are being displayed as 1.0, 2.0, 3.0, etc... I would like to remove > the decimal and any trailing zeros with out effecting other non-whole > number floats (ex. 1.375). > > Any ideas? Help appreciated.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Matt - I''ll try this out. Best, NB On Feb 27, 4:33 pm, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> The %g specifier to sprintf will do this automatically: > > "%g" % 1.0 > => "1" > > "%g" % 1.375 > => "1.375" > > --Matt Jones > > On Feb 27, 1:20 am, northband <northb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi - > > > I am working with floats and am wondering how I can trim the decimal > > and trailing zeros on my whole numbers. For instance, whole numbers > > are being displayed as 1.0, 2.0, 3.0, etc... I would like to remove > > the decimal and any trailing zeros with out effecting other non-whole > > number floats (ex. 1.375). > > > Any ideas? Help appreciated.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
And how can I remove the trailing zeros if the value is to be shown in a text field? I.e.: <%= linea_form.text_field :unidades, :id => "invoice_line_units", :name => "invoice[lines_attributes][#{id_or_index}][units]", :size => 10 %> Matt Jones wrote:> The %g specifier to sprintf will do this automatically: > > "%g" % 1.0 > => "1" > > "%g" % 1.375 > => "1.375" > > --Matt Jones-- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.