money 0.2.1

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 87

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 packages get

Alternatively, your editor might support pub get or flutter packages 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';
  
Version Uploaded Documentation Archive
0.2.1 Aug 21, 2018 Go to the documentation of money 0.2.1 Download money 0.2.1 archive
0.2.0 Aug 17, 2018 Go to the documentation of money 0.2.0 Download money 0.2.0 archive
0.1.6 Feb 24, 2017 Go to the documentation of money 0.1.6 Download money 0.1.6 archive
0.1.5 Jul 5, 2016 Go to the documentation of money 0.1.5 Download money 0.1.5 archive
0.1.4 Jun 3, 2016 Go to the documentation of money 0.1.4 Download money 0.1.4 archive
0.1.3+2 May 10, 2016 Go to the documentation of money 0.1.3+2 Download money 0.1.3+2 archive
0.1.3+1 May 6, 2015 Go to the documentation of money 0.1.3+1 Download money 0.1.3+1 archive
0.1.3 May 5, 2015 Go to the documentation of money 0.1.3 Download money 0.1.3 archive
0.1.2 May 5, 2015 Go to the documentation of money 0.1.2 Download money 0.1.2 archive
0.1.1 May 4, 2015 Go to the documentation of money 0.1.1 Download money 0.1.1 archive

All 12 versions...

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

We analyzed this package on Oct 10, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.4

Platforms

Detected platforms: Flutter, web, other

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

Health suggestions

Format lib/src/currencies.dart.

Run dartfmt to format lib/src/currencies.dart.

Maintenance suggestions

The description is too short. (-20 points)

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use money.dart.

Dependencies

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