Class FixedPoint

extends Object implements Comparable
An object that behaves like a number. It is implemented as a fixed-point decimal number with a given number of digits after the decimal point. Unlike a float it does not have rounding issues, so adding 0.1 to a number 10 times will add exactly 1 to it. When comparing a FixedPoint with a float or int, place the FixedPoint object on the left side of the comparison operator.

Class summary


constructor value/any --decimals/int=
Constructs a FixedPoint number from another number, or a string.

Constructors

constructor value/any --decimals/int=null
Constructs a FixedPoint number from another number, or a string.
If value is a string, then the string is parsed and the precision is taken from the number of digits after the decimal point, string, ignoring decimals.
If value is a floating point, then the result is rounded to the nearest fixed-point value, given the precision indicated in decimals, which defaults to 2.
If value is an int, the decimals defaults to 2.
If value is a FixedPoint, the precision of the result will be the max of decimals or the precision of value.

Statics

parse str/string from/int=0 to/int=str.size --radix/any=10 --decimals/int=null -> FixedPoint
Parse string as a signed decimal integer.
If decimals is not given, then the actual number of digits after the decimal point is used for the precision.

Methods

operator - other/any -> any
Subtraction operator.
Returns a float if other is a float.
Returns a FixedPoint if other is an int or a FixedPoint.

operator * other/any -> any
Multiplication operator.
Returns a float if other is a float.
Returns a FixedPoint if other is an int.
Does not support multiplying two FixedPoint values with each other.

operator / other/any -> any
Division operator.
Returns a float if other is a float.
Returns a FixedPoint if other is an int. Rounds down, like the division operator on ints.
Does not support dividing two FixedPoint values by each other.

Returns a FixedPoint. Eg. 3.14 % 2 == 1.14. This means it is not a counterpart of the / operator, but rather a counterpart of (x / y).to-int.

operator + other/any -> any
Addition operator.
Returns a float if other is a float.
Returns a FixedPoint if other is an int or a FixedPoint.

operator < other/any -> bool

operator <= other/any -> bool

operator == other/any -> bool

operator > other/any -> bool

operator >= other/any -> bool

Rounds down like the / operator.

with --decimals/int -> any
Returns a FixedPoint with a specific number of decimal digits.
Mainly useful for printing.
If the number of decimals is reduced, rounds to nearest.