Module: Kernel
- Defined in:
- sig/big_decimal.rbs
Instance Method Summary collapse
-
#BigDecimal(*args) ⇒ Object
Returns the BigDecimal converted from
valuewith a precision ofndigitsdecimal digits. -
#self?.BigDecimal ⇒ Object
Returns the BigDecimal converted from `value` with a precision of `ndigits` decimal digits.
Instance Method Details
#BigDecimal(value, exception: true) ⇒ Object #BigDecimal(value, ndigits, exception: true) ⇒ Object
Returns the BigDecimal converted from value
with a precision of ndigits decimal digits.
When ndigits is less than the number of significant digits
in the value, the result is rounded to that number of digits,
according to the current rounding mode; see BigDecimal.mode.
When ndigits is 0, the number of digits to correctly represent a float number
is determined automatically.
Returns value converted to a BigDecimal, depending on the type of value:
-
Integer, Float, Rational, Complex, or BigDecimal: converted directly:
# Integer, Complex, Float, or BigDecimal value does not require ndigits; ignored if given. BigDecimal(2) # => 0.2e1 BigDecimal(Complex(2, 0)) # => 0.2e1 BigDecimal(BigDecimal(2)) # => 0.2e1 BigDecimal(2.0) # => 0.2e1 # Rational value requires ndigits. BigDecimal(Rational(2, 1), 0) # => 0.2e1 -
String: converted by parsing if it contains an integer or floating-point literal; leading and trailing whitespace is ignored:
# String does not require ndigits; ignored if given. BigDecimal('2') # => 0.2e1 BigDecimal('2.0') # => 0.2e1 BigDecimal('0.2e1') # => 0.2e1 BigDecimal(' 2.0 ') # => 0.2e1 -
Other type that responds to method :to_str: first converted to a string, then converted to a BigDecimal, as above.
-
Other type:
-
Raises an exception if keyword argument
exceptionistrue. -
Returns
nilif keyword argumentexceptionisfalse.
Raises an exception if value evaluates to a Float
and digits is larger than Float::DIG + 1.
2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 |
# File 'ext/bigdecimal/bigdecimal.c', line 2978 static VALUE f_BigDecimal(int argc, VALUE *argv, VALUE self) { VALUE val, digs_v, opts = Qnil; argc = rb_scan_args(argc, argv, "11:", &val, &digs_v, &opts); int exception = opts_exception_p(opts); size_t digs = SIZE_MAX; /* this means digs is omitted */ if (argc > 1) { digs_v = rb_to_int(digs_v); if (FIXNUM_P(digs_v)) { long n = FIX2LONG(digs_v); if (n < 0) goto negative_digs; digs = (size_t)n; } else { if (RBIGNUM_NEGATIVE_P(digs_v)) { negative_digs: if (!exception) return Qnil; rb_raise(rb_eArgError, "negative precision"); } digs = NUM2SIZET(digs_v); } } return rb_convert_to_BigDecimal(val, digs, exception); } |
#self? ⇒ BigDecimal #self? ⇒ BigDecimal? #self? ⇒ BigDecimal?
Returns the BigDecimal converted from value with a precision of ndigits
decimal digits.
When ndigits is less than the number of significant digits in the value, the
result is rounded to that number of digits, according to the current rounding
mode; see BigDecimal.mode.
When ndigits is 0, the number of digits to correctly represent a float
number is determined automatically.
Returns value converted to a BigDecimal, depending on the type of value:
-
Integer, Float, Rational, Complex, or BigDecimal: converted directly:
# Integer, Complex, or BigDecimal value does not require ndigits; ignored if given. BigDecimal(2) # => 0.2e1 BigDecimal(Complex(2, 0)) # => 0.2e1 BigDecimal(BigDecimal(2)) # => 0.2e1 # Float or Rational value requires ndigits. BigDecimal(2.0, 0) # => 0.2e1 BigDecimal(Rational(2, 1), 0) # => 0.2e1 -
String: converted by parsing if it contains an integer or floating-point literal; leading and trailing whitespace is ignored:
# String does not require ndigits; ignored if given. BigDecimal('2') # => 0.2e1 BigDecimal('2.0') # => 0.2e1 BigDecimal('0.2e1') # => 0.2e1 BigDecimal(' 2.0 ') # => 0.2e1 -
Other type that responds to method
:to_str: first converted to a string, then converted to a BigDecimal, as above. -
Other type:
- Raises an exception if keyword argument
exceptionistrue. - Returns
nilif keyword argumentexceptionisfalse.
- Raises an exception if keyword argument
Raises an exception if value evaluates to a Float and digits is larger
than Float::DIG + 1.
1240 1241 1242 |
# File 'sig/big_decimal.rbs', line 1240 def self?.BigDecimal: (real | string | BigDecimal initial, ?int digits, ?exception: true) -> BigDecimal | (real | string | BigDecimal initial, ?int digits, exception: bool) -> BigDecimal? | (untyped initial, ?untyped digits, ?exception: bool) -> BigDecimal? |