method
to_int
v2_2_9 -
Show latest stable
- Class:
BigDecimal
to_int()public
Returns the value as an integer (Fixnum or Bignum).
If the BigNumber is infinity or NaN, raises FloatDomainError.
static VALUE
BigDecimal_to_i(VALUE self)
{
ENTER(5);
ssize_t e, nf;
Real *p;
GUARD_OBJ(p, GetVpValue(self, 1));
BigDecimal_check_num(p);
e = VpExponent10(p);
if (e <= 0) return INT2FIX(0);
nf = VpBaseFig();
if (e <= nf) {
return LONG2NUM((long)(VpGetSign(p) * (BDIGIT_DBL_SIGNED)p->frac[0]));
}
else {
VALUE a = BigDecimal_split(self);
VALUE digits = RARRAY_PTR(a)[1];
VALUE numerator = rb_funcall(digits, rb_intern("to_i"), 0);
VALUE ret;
ssize_t dpower = e - (ssize_t)RSTRING_LEN(digits);
if (VpGetSign(p) < 0) {
numerator = rb_funcall(numerator, '*', 1, INT2FIX(-1));
}
if (dpower < 0) {
ret = rb_funcall(numerator, rb_intern("div"), 1,
rb_funcall(INT2FIX(10), rb_intern("**"), 1,
INT2FIX(-dpower)));
}
else {
ret = rb_funcall(numerator, '*', 1,
rb_funcall(INT2FIX(10), rb_intern("**"), 1,
INT2FIX(dpower)));
}
if (RB_TYPE_P(ret, T_FLOAT)) {
rb_raise(rb_eFloatDomainError, "Infinity");
}
return ret;
}
} Related methods
- Instance methods
- %
- *
- **
- +
- +@
- -
- -@
- /
- <
- <=
- <=>
- ==
- ===
- >
- >=
- _dump
- abs
- add
- as_json
- ceil
- coerce
- div
- divmod
- 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