money 0.1.5 copy "money: ^0.1.5" to clipboard
money: ^0.1.5 copied to clipboard

outdatedDart 1 only

Dart implementation of Fowler's Money pattern

Money #

Dart implementation of Fowler's Money pattern.

Pub version License documentation codecov Build Status

Stories in Ready

Usage Examples #

Creating a Money object and accessing its monetary value #

// Create a money object that represents 1 USD
var money = new Money(100, new Currency('USD'));

// Access the Money object's monetary value
print(money.amount); // => 100

// -- OR --
print(money.amountAsString); // => 1.00

Creating a Money object from a string value

var money = new Money.fromString('12.50', new Currency('USD'));

print(money.amount); // => 1250

Creating a money from a double value

var money = new Money.fromDouble(12.34, new Currency('USD'));

print(money.amount); // => 1234

Simple conversion to a string #

var money = new Money(150, new Currency('USD'));

print(money.toString()); // => 1.50 USD

Basic arithmetic using Money objects #

// Create two Money objects that represent 1 USD and 2 USD, respectively
final a = new Money(100, new Currency('USD'));
final b = new Money(200, new Currency('USD'));

var c = null;

// Negate a Money object
c = -a;
print(c); // => -1.00 USD

// Calculate the sum of two Money objects
c = a + b;
print(c); // => 3.00 USD


// Calculate the difference of two Money objects
c = b - a;
print(c); // => 1.00 USD

// Multiply a Money object with a factor
c = a * 2;
print(c); // => 2.00 USD

Comparing Money objects #

final a = new Money(100, new Currency('USD'));
final b = new Money(200, new Currency('USD'));

a < b; // => true
a > b; // => false

b <= a; // => false
b => a; // => true

a.compareTo(b); // => -1
a.compareTo(a); // =>  0
b.compareTo(a); // =>  1

The compareTo() method returns an integer less than, equal to, or greater than zero if the value of one Money object is considered to be respectively less than, equal to, or greater than that of another Money object.

Money implements Comparable interface and you can sort a list of Money objects.

Allocate the monetary value represented by a Money object using a list of ratios #

final a = new Money(5, new Currency('USD'));

for (var c in a.allocate(3, 7)) {
  print(c);
}

The code above produces the output shown below:

2
3
13
likes
0
pub points
65%
popularity

Publisher

verified publisherlitgroup.ru

Dart implementation of Fowler's Money pattern

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

More

Packages that depend on money