Ruby 2.4.0 リファレンスマニュアル > ライブラリ一覧 > bigdecimalライブラリ > BigDecimalクラス

class BigDecimal

クラス・モジュールの継承リスト: BigDecimal < Numeric < Comparable < Object < Kernel < BasicObject

要約

可変長浮動小数点計算クラスです。

特異メソッド

定義 説明
_load(str) -> BigDecimal

引数で指定された文字列を元に BigDecimal オブジェクトを復元します。 Marshal.#load から呼び出されます。

double_fig -> Integer

Ruby の Float クラスが保持できる有効数字の数を返します。

limit(n = nil) -> Integer

生成されるBigDecimalオブジェクトの最大桁数をn桁に制限します。 n を指定しない、または n が nil の場合は、現状の最大桁数が返ります。

mode(s) -> Integer | nil
mode(s, v) -> Integer | nil

BigDecimal の計算処理の制御方法を設定、確認します。

new(s) -> BigDecimal
new(s, n) -> BigDecimal

このメソッドは Ruby 2.5 から deprecated です。Kernel.#BigDecimal を使用してください。

ver -> String

このメソッドは Ruby 2.5 から deprecated です。

インスタンスメソッド

定義 説明
modulo(n) -> BigDecimal
self % n -> BigDecimal

self を n で割った余りを返します。

self * other -> BigDecimal

積を計算します。

power(n) -> BigDecimal
power(n, prec) -> BigDecimal
self ** n -> BigDecimal

self の n 乗を計算します。

self + other -> BigDecimal

和を計算します。

+ self -> self

self を返します。

self - other -> BigDecimal

差を計算します。

- self -> BigDecimal

self の符号を反転させたものを返します。

div(other) -> BigDecimal
quo(other) -> BigDecimal
self / other -> BigDecimal

商を計算します。

self < other -> bool

self が other より小さい場合に true を、そうでない場合に false を返します。

self <= other -> bool

self が other より小さいか等しい場合に true を、そうでない場合に false を返します。

self <=> other -> -1 | 0 | 1 | nil

self が other より大きい場合に 1 を、等しい場合に 0 を、小さい場合には -1 をそれぞれ返します。

self == other -> bool
self === other -> bool
eql?(other) -> bool

self が other と等しい場合に true を、そうでない場合に false を返します。

self > other -> bool

self が other より大きい場合に true を、そうでない場合に false を返します。

self >= other -> bool

self が other より大きいか等しい場合に true を、そうでない場合に false を返します。

_dump -> String

BigDecimal._load で復元可能な文字列を返します。 Marshal.#dump から呼び出されます。

abs -> BigDecimal

self の絶対値を返します。

add(other, n) -> BigDecimal

和を計算します。

ceil -> Integer
ceil(n) -> BigDecimal

self 以上の整数のうち、最も小さい整数を計算し、その値を返します。

coerce(other) -> Array

self と other が同じクラスになるよう、self か other を変換し [other, self] という配列にして返します。

div(other, n) -> BigDecimal

商を計算します。

divmod(n) -> [BigDecimal, BigDecimal]

self を other で割った商 q と余り r を、 [q, r] という 2 要素の配列にして返します。

exponent -> Integer

self の指数部を整数値で返します。

finite? -> bool

self が ∞または NaN でないときに true を返します。それ以外のときに false を返します。

fix -> BigDecimal

self の整数部分を新しい BigDecimal オブジェクトにして返します。

floor -> Integer
floor(n) -> BigDecimal

self 以下の最大整数を返します。

frac -> BigDecimal

self の小数部分を新しい BigDecimal オブジェクトにして返します。

hash -> Integer

self のハッシュ値を返します。

infinite? -> 1 | -1 | nil

self が +∞のときに 1 、-∞のときに-1、それ以外のときに nil を返します。

inspect -> String

BigDecimal オブジェクトを表す文字列を返します。

mult(other, n) -> BigDecimal

積を計算します。

nan? -> bool

self が NaN のときに true を返します。それ以外のときに false を返します。

nonzero? -> self | nil

self が 0 以外のときに self を返します。0 のときに nil を返します。

precs -> [Integer, Integer]

self の有効数字と最大有効数字の配列を返します。

remainder(n) -> BigDecimal

self を n で割った余りを返します。

round -> Integer
round(n) -> BigDecimal
round(n, b) -> BigDecimal

クラスメソッド BigDecimal.mode(BigDecimal::ROUND_MODE,flag) で指定した BigDecimal::ROUND_MODE に従って丸め操作を実行します。

save_exception_mode { ... } -> object

例外処理に関する BigDecimal.mode の設定を保存してブロックを評価します。ブロック中で変更した設定はブロックの評価後に復元されます。

save_limit { ... } -> object

現在の BigDecimal.limit の設定を保存してブロックを評価します。ブロック中で変更した設定はブロックの評価後に復元されます。

save_rounding_mode { ... } -> object

丸め処理に関する BigDecimal.mode の設定を保存してブロックを評価します。ブロック中で変更した設定はブロックの評価後に復元されます。

sign -> -3 | -2 | -1 | 0 | 1 | 2 | 3

自身の符号等の性質に応じて、Integer を返します。

split -> [Integer, String, Integer, Integer]

BigDecimal 値を 0.xxxxxxx*10**n と表現したときに、符号 (NaNのときは 0、それ以外は+1か-1になります)、仮数部分の文字列("xxxxxxx")と、基数(10)、更に指数 n を配列で返します。

sqrt(n) -> BigDecimal

self の有効桁 n 桁の平方根 (n の平方根ではありません) をニュートン法で計算します。

sub(other, n) -> BigDecimal

差を計算します。

to_f -> Float

self の近似値を表す Float オブジェクトに変換します。

to_i -> Integer
to_int -> Integer

self の小数点以下を切り捨てて整数に変換します。

to_r -> Rational

self を Rational オブジェクトに変換します。

to_s -> String
to_s(n) -> String

self を文字列に変換します (デフォルトは "0.xxxxxen" の形になります)。

truncate -> Integer
truncate(n) -> BigDecimal

小数点以下の数を切り捨てて整数にします。

zero? -> bool

self が 0 のときに true を返します。それ以外のときに false を返します。

定数

定義 説明
BASE -> Integer

ライブラリ内部で使用します。

EXCEPTION_ALL -> Integer

BigDecimal の計算において例外を発生させるかどうかを設定、確認する際の値を返します。

EXCEPTION_INFINITY -> Integer

BigDecimal の計算結果が無限大になった場合に例外を発生させるかどうかを設定、確認する際の値を返します。

EXCEPTION_NaN -> Integer

BigDecimal の計算結果が NaN になった場合に例外を発生させるかどうかを設定、確認する際の値を返します。

EXCEPTION_OVERFLOW -> Integer

BigDecimal の計算結果の指数部がオーバーフローした場合に例外を発生させるかどうかを設定、確認する際の値を返します。

EXCEPTION_UNDERFLOW -> Integer

BigDecimal の計算結果の指数部がアンダーフローした場合に例外を発生させるかどうかを設定、確認する際の値を返します。

EXCEPTION_ZERODIVIDE -> Integer

BigDecimal に 0 による割り算を実行した場合に例外を発生させるかどうかを設定、確認する際の値を返します。

INFINITY -> BigDecimal

正の無限大を表す BigDecimal オブジェクトを返します。

NAN -> BigDecimal

NaN を表す BigDecimal オブジェクトを返します。

ROUND_CEILING -> Integer

BigDecimal の計算結果の丸め処理で数値の大きい方に繰り上げるかどうかを設定、確認する際の値を返します。

ROUND_DOWN -> Integer

BigDecimal の計算結果の丸め処理で全て切り捨てるかどうかを設定、確認する際の値を返します。

ROUND_FLOOR -> Integer

BigDecimal の計算結果の丸め処理で数値の小さい方に繰り下げるかどうかを設定、確認する際の値を返します。

ROUND_HALF_DOWN -> Integer

BigDecimal の計算結果の丸め処理で五捨六入するかどうかを設定、確認する際の値を返します。

ROUND_HALF_EVEN -> Integer

BigDecimal の計算結果の丸め処理で四捨六入するかどうかを設定、確認する際の値を返します。5の時は上位1桁が奇数の時のみ繰り上げます (Banker's rounding)。

ROUND_HALF_UP -> Integer

BigDecimal の計算結果の丸め処理で四捨五入するかどうかを設定、確認する際の値を返します。

ROUND_MODE -> Integer

BigDecimal の計算結果の丸め処理を設定、確認する際の値を返します。

ROUND_UP -> Integer

BigDecimal の計算結果の丸め処理で全て切り上げするかどうかを設定、確認する際の値を返します。

SIGN_NEGATIVE_FINITE -> Integer

負の値に対応する BigDecimal#sign の値を返します。

SIGN_NEGATIVE_INFINITE -> Integer

負の無限大に対応する BigDecimal#sign の値を返します。

SIGN_NEGATIVE_ZERO -> Integer

負の 0 に対応する BigDecimal#sign の値を返します。

SIGN_NaN -> Integer

NaN に対応する BigDecimal#sign の値を返します。

SIGN_POSITIVE_FINITE -> Integer

正の値に対応する BigDecimal#sign の値を返します。

SIGN_POSITIVE_INFINITE -> Integer

正の無限大に対応する BigDecimal#sign の値を返します。

SIGN_POSITIVE_ZERO -> Integer

正の 0 に対応する BigDecimal#sign の値を返します。

継承したメソッド

! != __id__ __send__ instance_eval instance_exec method_missing singleton_method_added singleton_method_removed singleton_method_undefined abs abs2 angle conj denominator fdiv i imag integer? negative? numerator polar positive? real real? rect step to_c !~ =~ class clone define_singleton_method display enum_for equal? extend freeze frozen? initialize initialize_copy instance_of? instance_variable_defined? instance_variable_get instance_variable_set instance_variables is_a? itself marshal_dump marshal_load method methods nil? object_id pretty_inspect pretty_print pretty_print_cycle pretty_print_inspect pretty_print_instance_variables private_methods protected_methods psych_to_yaml public_method public_methods public_send remove_instance_variable respond_to? respond_to_missing? send singleton_class singleton_method singleton_methods taint tainted? tap to_a to_ary to_hash to_io to_proc to_regexp to_str trust untaint untrust untrusted? .yaml_tag ::ARGF ::ARGV ::DATA ::ENV ::FALSE ::NIL ::RUBY_COPYRIGHT ::RUBY_DESCRIPTION ::RUBY_ENGINE ::RUBY_ENGINE_VERSION ::RUBY_PATCHLEVEL ::RUBY_PLATFORM ::RUBY_RELEASE_DATE ::RUBY_REVISION ::RUBY_VERSION ::SCRIPT_LINES__ ::STDERR ::STDIN ::STDOUT ::TOPLEVEL_BINDING ::TRUE

追加されるメソッド

定義 説明 ライブラリ
json_create(hash) -> BigDecimal

JSON のオブジェクトから BigDecimal のオブジェクトを生成して返します。

json/add/bigdecimal
to_d -> BigDecimal

自身を返します。

bigdecimal/util
to_digits -> String

自身を "1234.567" のような十進数の形式にフォーマットした文字列に変換します。

bigdecimal/util
to_json(*args) -> String

自身を JSON 形式の文字列に変換して返します。

json/add/bigdecimal