Forum: Ruby can't convert string to float

Ccd9be37f5bd322e9d3968f767328928?d=identicon&s=25 Sebastjan H. (sebastjan_h)
on 2014-05-02 20:06
Hi guys,

I'm having trouble converting string to float.

I get the user input in a html form (input type is text) and then I
have:

input.no_tus.to_f / 100.0

The conversion doesn't happen. For example:

I had input.no_tus to be 56 and the result for the above code was 0.

Any thoughts?

I tried in irb and it works ok.

This is a part of a camping web app.

regards,
seba
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2014-05-02 20:10
Your code is fine.

> input = '5'
> input.to_f / 100.0

Result:

# => 0.05
Ccd9be37f5bd322e9d3968f767328928?d=identicon&s=25 Sebastjan H. (sebastjan_h)
on 2014-05-02 20:21
Robert Heiler wrote in post #1144732:
> Your code is fine.
>
>> input = '5'
>> input.to_f / 100.0
>
> Result:
>
> # => 0.05


I know:(
However, something is causing the to_f not to happen. I have similar
problems with calculating the weighted value:

(input.addition_omission.to_f * 3.0) + (input.term_base.to_f * 3.0) +
(input.transl_mem.to_f * 2.0)

I left others out for brevity, but they're the same. Here, the input is
html type number and the output is always integer, never float.

regards,
seba
Ccd9be37f5bd322e9d3968f767328928?d=identicon&s=25 Sebastjan H. (sebastjan_h)
on 2014-05-02 20:47
Sebastjan H. wrote in post #1144734:
> Robert Heiler wrote in post #1144732:
>> Your code is fine.
>>
>>> input = '5'
>>> input.to_f / 100.0
>>
>> Result:
>>
>> # => 0.05
>
>
> I know:(
> However, something is causing the to_f not to happen. I have similar
> problems with calculating the weighted value:
>
> (input.addition_omission.to_f * 3.0) + (input.term_base.to_f * 3.0) +
> (input.transl_mem.to_f * 2.0)
>
> I left others out for brevity, but they're the same. Here, the input is
> html type number and the output is always integer, never float.
>
> regards,
> seba

The error was in active record types. It works fine now.

regards
seba
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.