0 purchases
currency type
currency_type #
The Currency type is a large numeric type, with exactly four digit after the decimal point.
Internally, Currency using integer calculation to minimizes rounding errors.
The Currency type is appropriate for financial calculations that require large numbers of significant integral, up to four fractional digits,
and minimize error caused by floating point calculation.
Why Using Currency ? #
Using Double
func main() {
var a = 0.7;
var b = 0.49;
var c = aa * aa;
print('Result : $c');
if (c == b) {
print("Yes it's equal");
} else {
print("No it's not equal");
}
}
copied to clipboard
Result : 0.48999999999999994
No it's not equal
copied to clipboard
Using Currency
func main() {
var a = Currency.parse('0.7');
var b = Currency.parse('0.49');
var c = a * a;
print('Result : $c');
if (c == b) {
print("Yes it's equal");
} else {
print("No it's not equal");
}
}
copied to clipboard
Result : 0.4900
Yes it's equal
copied to clipboard
How To Use #
To use currency_type module, you have to add to your project by run following command:
$ dart pub add currency_type
copied to clipboard
or add directly in pubspec.yaml
dependencies:
currency_type: ^0.0.2
copied to clipboard
and then import to your dart file
import 'package:currency_type/currency_type.dart';
copied to clipboard
Instantiate #
You can instantiate Currency with 3 ways:
Create zero value
var a = Currency();
copied to clipboard
Import from num varible or num literal :
var b = 1234;
var c = 1234.56;
var d = Currency.from(b);
var e = Currency.from(c);
var f = Currency.from(1234);
var g = Currency.from(1234.56);
copied to clipboard
Parsing from string :
var f = Currency.parse('1234567890');
var g = Currency.parse('1234567890123456.1234');
copied to clipboard
Currency can store very large number beyond double, and the only way to instantiate is by parsing from string.
Operation #
You can do arithmetic operation on `Currency` like addition, subtraction, multiplication, or division
var h = d + e;
var i = g - h;
var j = d * e;
var k = g / f;
var l = (d + e) * g;
copied to clipboard
Currency can also be compared to other Currency
if (k == l) {
...
}
if (h > Currency.from(100)) {
...
}
copied to clipboard
Copyright (c) 2021, Mohamad Tantowi Mustofa (tantowi.com)
Licensed under BSD 3-Clause license.
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.