std:decimal リファレンス
@import 'std:decimal' で取得する 10 進小数モジュール。正確な 10 進小数の値型 Decimal と、生成 (of / parse)・算術・丸め (5 モード) を提供する。
Ceiling
Ceiling Ceiling: RoundingMode
丸めモード: +∞方向。
>>> decimal.Ceiling Ceiling
Floor
Floor Floor: RoundingMode
丸めモード: −∞方向。
>>> decimal.Floor Floor
HalfEven
HalfEven HalfEven: RoundingMode
丸めモード: 最近接・半数は偶数側 (banker's)。
>>> decimal.HalfEven HalfEven
HalfUp
HalfUp HalfUp: RoundingMode
丸めモード: 最近接・半数は 0 から遠い側 (四捨五入)。
>>> decimal.HalfUp HalfUp
TowardZero
TowardZero TowardZero: RoundingMode
丸めモード: 0 方向 (切り捨て)。
>>> decimal.TowardZero TowardZero
of
of(n)
of: { Int | Decimal }
Integer n を正確に Decimal 化する。
>>> decimal.of(1500) 1500
parse
parse(s)
parse: { String | Option(Decimal) }
String を 10 進表記として解析し Option(Decimal) を返す (不能は None)。小数は
これで作る。
>>> decimal.parse("0.1") Some(0.1)
Decimal メソッド
abs
dec.abs! abs: { Decimal }
絶対値。
add
dec.add(other) add: { Decimal | Decimal }
和 (正確)。
compare
dec.compare(other) compare: { Decimal | Ordering }
値の全順序。
div
dec.div(other, places, mode) div: { Decimal, Int, RoundingMode | Decimal }
商を places 桁に mode で丸める。除数 0 はエラー。
div_exact
dec.div_exact(other) div_exact: { Decimal | Option(Decimal) }
有限小数で割り切れれば Some、循環等で不能なら None。除数 0 はエラー。
format
dec.format(places, mode) format: { Int, RoundingMode | String }
places 桁ちょうどの固定桁文字列 (表示用)。
mul
dec.mul(other) mul: { Decimal | Decimal }
積 (正確)。
neg
dec.neg! neg: { Decimal }
符号反転。
round
dec.round(places, mode) round: { Int, RoundingMode | Decimal }
places 桁に mode で丸めた値 (正準形)。
sign
dec.sign! sign: { Int }
符号 (負 -1・零 0・正 1) を Integer で返す。
sub
dec.sub(other) sub: { Decimal | Decimal }
差 (正確)。
to_float
dec.to_float! to_float: { Option(Float) }
最近接 Float を Option で返す (範囲超は None)。
to_int
dec.to_int(mode) to_int: { RoundingMode | Int }
整数へ mode で丸めて Integer を返す。
to_string
dec.to_string! to_string: { String }
正準形の 10 進文字列。