money 0.2.1

  • Readme
  • Changelog
  • Installing
  • 83

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

Change Log #

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

0.2.1 - 2018-08-21 #

Fixed #

  • Fixes comparison of 0 and -0 amount in a browser.

0.2.0 - 2018-08-17 #

Changed #

  • Code was migrated to Dart 2.0. No API changes.

0.1.6 - 2017-02-24 #

Fixed #

  • Fixed wrong parsing from string when integer part of amount is 0.

0.1.5 - 2016-07-06 #

Changed #

  • Class Currency is not abstract from now on.

Fixed #

  • Money.hashCode now relates on amount and currency (Issue #1).

0.1.4 - 2016-06-03 #

Changed #

  • [BC] Money.==() now receives Object instead of Money and checks runtime type of the argument, closes #4.

0.1.3+2 - 2016-05-10 #

Fixed #

  • Fixed invalid rounding of amount in Money.toString(), closes #3.

0.1.3 - 2015-05-05 #

Added #

  • Added Money.fromDouble() constructor.

0.1.2 - 2015-05-05 #

Added #

  • Added getter Money.amountAsString.

0.1.1 - 2015-05-04 #

Added #

  • Added Money.fromString() constructor.
  • Added relational operators (<, <=, >, >=).

0.1.0+1 - 2015-05-01 #

Fixed #

  • Fixes README.md.

[0.1.0] - 2015-05-01

Initial version.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  money: ^0.2.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:money/money.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
87
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
51
Overall:
Weighted score of the above. [more]
83
Learn more about scoring.

We analyzed this package on Nov 16, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.0
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:money/money.dart.

Health suggestions

Fix lib/src/currencies.dart. (-0.50 points)

Analysis of lib/src/currencies.dart reported 1 hint:

line 26 col 43: Avoid const keyword.

Maintenance suggestions

Package is getting outdated. (-23.84 points)

The package was last published 64 weeks ago.

The package description is too short. (-15 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and money.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
test >=1.3.0 <2.0.0