Fractional part of a float

J

Jari Williamsson

Is there any built-in method to get the fractional part from a float, or
do I need to resolve to a...
f - f.to_i
...kind of approach?


Best regards,

Jari Williamsson
 
7

7stud --

Jari said:
Is there any built-in method to get the fractional part from a float, or
do I need to resolve to a...
f - f.to_i
...kind of approach?

If you think it's clearer:

8.2675.modulo(1)
 
W

Wayne Vucenic

8.2675.modulo(1)

irb(main):015:0> -1.2.modulo(1)
=> 0.8

which may or may not be the result the OP wanted.

Wayne
 
L

Lloyd Linklater

7stud said:
If you think it's clearer:

8.2675.modulo(1)

It sounds as if we need a new method for numbers, mantissa, where the
fraction portion is returned. Can we add that to the core?
 
S

Stefan Rusterholz

Wayne said:
irb(main):015:0> -1.2.modulo(1)
=> 0.8

which may or may not be the result the OP wanted.

Wayne

-1.2.abs.modulo(1) # => 0.2

Not all that difficult to avoid if it is not what the OP wanted.

Regards
Stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,647
Latest member
NelleMacy9

Latest Threads

Top