本文へ移動
mie リファレンス
REPL Playground

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 }

絶対値。

>>> decimal.parse("-2.5").unwrap!.abs!
2.5

add

dec.add(other)
add: { Decimal | Decimal }

和 (正確)。

>>> decimal.parse("0.1").unwrap!.add(decimal.parse("0.2").unwrap!)
0.3

compare

dec.compare(other)
compare: { Decimal | Ordering }

値の全順序。

>>> decimal.of(1).compare(decimal.of(2))
Less

div

dec.div(other, places, mode)
div: { Decimal, Int, RoundingMode | Decimal }

商を places 桁に mode で丸める。除数 0 はエラー。

>>> decimal.of(10).div(decimal.of(3), 4, decimal.HalfEven)
3.3333

div_exact

dec.div_exact(other)
div_exact: { Decimal | Option(Decimal) }

有限小数で割り切れれば Some、循環等で不能なら None。除数 0 はエラー。

>>> decimal.of(10).div_exact(decimal.of(4))
Some(2.5)

format

dec.format(places, mode)
format: { Int, RoundingMode | String }

places 桁ちょうどの固定桁文字列 (表示用)。

>>> decimal.of(1).format(2, decimal.HalfUp)
"1.00"

mul

dec.mul(other)
mul: { Decimal | Decimal }

積 (正確)。

>>> decimal.parse("19.99").unwrap!.mul(decimal.of(3))
59.97

neg

dec.neg!
neg: { Decimal }

符号反転。

>>> decimal.parse("2.5").unwrap!.neg!
-2.5

round

dec.round(places, mode)
round: { Int, RoundingMode | Decimal }

places 桁に mode で丸めた値 (正準形)。

>>> decimal.parse("1.005").unwrap!.round(2, decimal.HalfUp)
1.01

sign

dec.sign!
sign: { Int }

符号 (負 -1・零 0・正 1) を Integer で返す。

>>> decimal.parse("-2.5").unwrap!.sign!
-1

sub

dec.sub(other)
sub: { Decimal | Decimal }

差 (正確)。

>>> decimal.of(5).sub(decimal.parse("5.5").unwrap!)
-0.5

to_float

dec.to_float!
to_float: { Option(Float) }

最近接 Float を Option で返す (範囲超は None)。

>>> decimal.parse("1.5").unwrap!.to_float!
Some(1.5)

to_int

dec.to_int(mode)
to_int: { RoundingMode | Int }

整数へ mode で丸めて Integer を返す。

>>> decimal.parse("2.5").unwrap!.to_int(decimal.HalfEven)
2

to_string

dec.to_string!
to_string: { String }

正準形の 10 進文字列。

>>> decimal.parse("1.50").unwrap!.to_string!
"1.5"