[go: up one dir, main page]

Skip to content

Dart implementation of Fowler's Money pattern.

License

Notifications You must be signed in to change notification settings

haarts/money.dart

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

71 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Money

Dart implementation of Fowler's Money pattern.

Pub version License documentation Build Status

Usage Examples

Creating a Money object and accessing its monetary value

// Create a money object that represents 1 USD
final money = Money(100, 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

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

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

Creating a money from a double value

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

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

Simple conversion to a string

final money = Money(150, 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 = Money(100, Currency('USD'));
final b = Money(200, 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 = Money(100, Currency('USD'));
final b = Money(200, 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 = Money(5, Currency('USD'));

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

The code above produces the output shown below:

2
3

About

Dart implementation of Fowler's Money pattern.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Dart 100.0%