method
to_r
v2_5_5 -
Show latest stable
- Class:
BigDecimal
to_r()public
Converts a BigDecimal to a Rational.
static VALUE
BigDecimal_to_r(VALUE self)
{
Real *p;
ssize_t sign, power, denomi_power;
VALUE a, digits, numerator;
p = GetVpValue(self, 1);
BigDecimal_check_num(p);
sign = VpGetSign(p);
power = VpExponent10(p);
a = BigDecimal_split(self);
digits = RARRAY_AREF(a, 1);
denomi_power = power - RSTRING_LEN(digits);
numerator = rb_funcall(digits, rb_intern("to_i"), 0);
if (sign < 0) {
numerator = rb_funcall(numerator, '*', 1, INT2FIX(-1));
}
if (denomi_power < 0) {
return rb_Rational(numerator,
rb_funcall(INT2FIX(10), rb_intern("**"), 1,
INT2FIX(-denomi_power)));
}
else {
return rb_Rational1(rb_funcall(numerator, '*', 1,
rb_funcall(INT2FIX(10), rb_intern("**"), 1,
INT2FIX(denomi_power))));
}
} Related methods
- Instance methods
- %
- *
- **
- +
- +@
- -
- -@
- /
- <
- <=
- <=>
- ==
- ===
- >
- >=
- _dump
- abs
- add
- as_json
- ceil
- clone
- coerce
- div
- divmod
- dup
- eql?
- exponent
- finite?
- fix
- floor
- frac
- hash
- infinite?
- initialize_copy
- inspect
- modulo
- mult
- nan?
- nonzero?
- power
- precs
- quo
- remainder
- round
- sign
- split
- sqrt
- sub
- to_d
- to_digits
- to_f
- to_i
- to_int
- to_json
- to_r
- to_s
- truncate
- zero?
- Class methods
- _load
- double_fig
- json_create
- limit
- mode
- new
- save_exception_mode
- save_limit
- save_rounding_mode
- ver