Decimal class abstract
Decimal number.
Why?
Decimal numbers enable avoiding numerical issues with double. For example, CurrencyAmount uses Decimal to prevent numerical errors causing wrong prices.
Things to know
Internally decimal numbers are represented as a fraction of two integers nominator / denominator, where denominator is some power of 10 (1, 10, 100, 1000, etc.).
You need to define the number of decimal digits you need. This is possible with the method usingFractionDigits. Operations will return decimal numbers with N fraction digits, where N is the highest number of fraction digits in the arguments.
For example:
import package:kind/kind.dart';
void main() {
// Declare 1 and 3.
final one = Decimal('1.00'); // <-- 2 digits
final three = Decimal('3').usingFractionDigits(5); // <-- 5 digits
// Calculate 1/3 with 5 digits after dot.
print(one / three);
// --> 3.33333
}
- Implemented types
Constructors
Properties
- denominator → int
-
Denominator (parameter
b
ina/b
).no setter - fractionDigits → int
-
Number of digits after dot (".").
no setter
- hashCode → int
-
The hash code for this object.
no setteroverride
- isNegative → bool
-
Whether the value is negative.
no setter
- nominator → int
-
Nominator (parameter
a
ina/b
).no setter - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
ceil(
) → int - Calculates the lowest integer greater than or equal to this.
-
clamp(
Decimal min, Decimal max) → Decimal -
compareTo(
Decimal other) → int -
Compares this object to another object.
override
-
floor(
) → int - Calculates the highest integer less than or equal to this.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
round(
) → int - Rounds the decimal number.
-
scale(
num value) → Decimal -
toDouble(
) → double - Returns this as a floating-point value.
-
toInt(
) → int - Returns floor().
-
toString(
) → String -
A string representation of this object.
override
-
usingFractionDigits(
int fractionDigits) → Decimal
Operators
-
operator *(
Decimal other) → Decimal - Calculates product of this and the argument.
-
operator +(
Decimal other) → Decimal - Calculates sum of this and the argument.
-
operator -(
Decimal other) → Decimal - Calculates difference of this and the argument.
-
operator /(
Decimal other) → Decimal - Calculates fraction of this and the argument.
-
operator <(
Decimal other) → bool -
operator <=(
Decimal other) → bool -
operator ==(
Object other) → bool -
The equality operator.
override
-
operator >(
Decimal other) → bool -
operator >=(
Decimal other) → bool -
operator unary-(
) → Decimal - Negates the decimal number.