mirror of
https://github.com/moparisthebest/android.moparisthebest.org
synced 2024-11-01 15:55:03 -04:00
21 lines
397 B
Ruby
21 lines
397 B
Ruby
|
class Float
|
||
|
def number_decimal_places
|
||
|
self.to_s.length-2
|
||
|
end
|
||
|
def to_fraction
|
||
|
higher = 10**self.number_decimal_places
|
||
|
lower = self*higher
|
||
|
gcden = greatest_common_divisor(higher, lower)
|
||
|
|
||
|
return (lower/gcden).round, (higher/gcden).round
|
||
|
end
|
||
|
|
||
|
private
|
||
|
def greatest_common_divisor(a, b)
|
||
|
while a%b != 0
|
||
|
a,b = b.round,(a%b).round
|
||
|
end
|
||
|
return b
|
||
|
end
|
||
|
end
|