Feature #8850
openConvert Rational to decimal string
Description
On Ruby 2.1.0, decimal literal is introduced.
It generates Rational but it cannot easily convert to decimal string.
You know, Rational#to_f is related to this but
-
Float is not exact number
** 0.123456789123456789r.to_f.to_s #=> "0.12345678912345678" -
it can't handle recursive decimal
** (151/13r).to_f.to_s #=> "11.615384615384615" -
the method name
** to_decimal
** to_decimal_string
** to_s(format: :decimal)
** extend sprintf -
how does it express recursive decimal
** (151/13r).to_decimal_string #=> "11.615384..."
** (151/13r).to_decimal_string #=> "11.(615384)"
Example implementation is following.
Its result is
** 0.123456789123456789r.to_f.to_s #=> "0.123456789123456789"
** (151/13r).to_f.to_s #=> "11.(615384)"
class Rational
def to_decimal_string(base=10)
n = numerator
d = denominator
r, n = n.divmod d
str = r.to_s(base)
return str if n == 0
h = {}
str << '.'
n *= base
str.size.upto(Float::INFINITY) do |i|
r, n = n.divmod d
if n == 0
str << r.to_s(base)
break
elsif h.key? n
str[h[n], 0] = '('
str << ')'
break
else
str << r.to_s(base)
h[n] = i
n *= base
end
end
str
end
end
Updated by Anonymous about 11 years ago
How about to_s( :decimal ) ?
Updated by nobu (Nobuyoshi Nakada) almost 11 years ago
- Description updated (diff)
Updated by shyouhei (Shyouhei Urabe) almost 11 years ago
- Status changed from Assigned to Feedback
Can anyone propose a patch please?
Updated by mrkn (Kenta Murata) almost 11 years ago
I made a simple implementation: https://github.com/mrkn/ruby/commit/408d4a05c40a70a7d49e40d6edc33af98e6816a3
Updated by mrkn (Kenta Murata) almost 11 years ago
- Status changed from Feedback to Assigned
Updated by mrkn (Kenta Murata) almost 11 years ago
I updated the patch to use rb_check_arity: https://github.com/mrkn/ruby/commit/a6837fab190fed2707ec7e13c494cd4b642ff630
Updated by mrkn (Kenta Murata) almost 11 years ago
I've updated the patch by rebasing and fixing syntax.
https://github.com/mrkn/ruby/compare/64634f92f3474babab93264b3c7557043f6753e7...71e463b80d17959f3a4cfd49d7773818fa671037.diff