struct

Fixed(N)

Inherits Comparable < Comparable < Comparable < FixedDecimal::Value < Struct < Value < Object

A signed binary fixed-point number stored in an Int128.

The low N bits hold the fraction and the high 128 - N bits hold the integer part, including the sign, so the value is raw / 2**N. N must be in 0..127.

price = Fixed(64).new("19.99")
(price * 3).format(decimal: 2) # => "59.97"

Constructors

epsilon

The smallest positive value, one unit in the last place.

Source
from_sign_magnitude(negative : Bool, magnitude : UInt128 | Nil) : self

Builds a value from a sign and a magnitude in units of the least significant bit. Raises OverflowError when out of range, or when magnitude is nil (the convention for an intermediate overflow).

Source
max

The most positive value, 2**(127 - N) - 2**-N.

Source
min

The most negative value, -2**(127 - N).

Source
new(value : BigInt) : self

Converts exactly. Raises OverflowError if it is out of range.

Source
new(value : Int)

Converts an integer exactly. Raises OverflowError if it is out of range.

Source
new(value : BigFloat) : self

Converts, rounding half-to-even. Raises OverflowError if it is out of range.

Source
new(value : Float)

Converts a float, rounding half-to-even. Raises ArgumentError for NaN or infinity and OverflowError if it is out of range.

Source
new(value : String)

Parses a decimal string; see .parse.

Source
new(value : FixedDecimal::Value)

Converts from any Fixed or UFixed, rounding half-to-even when fraction bits are lost. Raises OverflowError if it is out of range.

Source
new(value : BigRational) : self

Converts, rounding half-to-even. Raises OverflowError if it is out of range.

Source
new(value : BigDecimal) : self

Converts, rounding half-to-even. Raises OverflowError if it is out of range.

Source
new(*, raw : Int128)

Wraps raw storage directly.

Source
one

One. Raises OverflowError when one is not representable.

Source
parse(string : String, *, whitespace : Bool = true) : self

Parses a decimal string such as "-12.375", "1_000.5", ".5" or "6.02e23", rounding half-to-even to the nearest representable value.

Raises ArgumentError if string is not a valid decimal number and OverflowError if it is out of range. Leading and trailing whitespace (ASCII only, as with String#to_i) is ignored when whitespace is true.

Source
zero

Zero.

Source

Class methods

fraction_bits

The number of fraction bits, N.

Source
integer_bits

The number of integer bits, including the sign bit if there is one.

Source
parse?(string : String, *, whitespace : Bool = true) : self | Nil

Like .parse, but returns nil instead of raising.

Source

Instance methods

raw

The two's complement storage; the value is raw / 2**N.

Source
sign_magnitude

The value as {negative, magnitude}, with the magnitude in units of the least significant bit.

Source