Hi, I want to convert a string to a Float value:
irb> “1.5”.to_f
=> 1.5
But if I do:
irb> “1”.to_f
=> 1.0
I get ‘1.0’ instead of just ‘1’. Is not possible to just get ‘1’ in this
case?
Thanks a lot.
Hi, I want to convert a string to a Float value:
irb> “1.5”.to_f
=> 1.5
But if I do:
irb> “1”.to_f
=> 1.0
I get ‘1.0’ instead of just ‘1’. Is not possible to just get ‘1’ in this
case?
Thanks a lot.
On Jul 1, 2008, at 4:09 PM, Iñaki Baz C. wrote:
I get ‘1.0’ instead of just ‘1’. Is not possible to just get ‘1’ in
this case?Thanks a lot.
–
Iñaki Baz C.
n = Integer(n) rescue Float(n)
Iñaki Baz C. wrote:
I get ‘1.0’ instead of just ‘1’. Is not possible to just get ‘1’ in this case?
Well, you could do this:
a = [“1”, “1.5”].map do |x|
Float x # validate
eval x
end
p a
Just a thought…
Numeric(“1.5”).should_return(1.5)
Numeric(“1”).should_return(1)
El Miércoles, 2 de Julio de 2008, Joel VanderWerf escribió:
Just a thought…
Numeric(“1.5”).should_return(1.5)
Numeric(“1”).should_return(1)
Thanks to all for your fast replies
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs