Giter Club home page Giter Club logo

fixed's Introduction

The Fixed package allows you to store and perform maths on numbers with a fixed scale (fixed no. of decimal places).

All amounts are store as integers to allow precision maths to be performed.

The Fixed package allows you to have explicit control over the no. of decimals (the scale) that are stored.

Fixed instances are immutable.

After a mathematical operation you may need to build a new Fixed object with the required scale.

For multiplication the resulting scale is the sum of the two scales e.g.

  0.01 * 0.02 = 0.0002
final rate = Fixed.fromMinorUnits(75486, scale: 5); // == 0.75486
final auDollars = Fixed.fromMinorUnits(100, scale: 2); // == 1.00
final usDollarsHighScale = auDollars * rate;  // ==0.7548600, scale = 7

/// reduce the scale to 2 decimal places.
final usDollars = Fixed(usDollarsHighScale, scale: 2); // == 0.75

When performing division the results scale will be the larger scale of the two operands.

final winnings = Fixed.fromMinorUnits(600000, scale: 5); // == 6.00000
final winners = Fixed.fromMinorUnits(200, scale: 2); // == 2.00
final share = winnings / winners;  // == 3.00000, scale = 5

Constructors

There are multiple ways you can create a Fixed object

final t2 = Fixed.fromMinorUnits(1234, scale: 3); // == 1.234
final t3 = Fixed.fromBigInt(BigInt.from(1234), scale: 3)); // == 1.234
final t4 = Fixed(t1); // == 1.234
final t5 = Fixed.parse('1.234', scale: 3); // == 1.234

// This is the least desireable method as it can introduce
// rounding errors.
final t6 = Fixed.from(1.234, scale: 3); // == 1.234

Operators

Fixed provides mathematical operations:

final t1 = Fixed.parse('1.23'); // = 1.23
final t2 = Fixed.fromMinorUnits(100, scale: 2); // = 1.00

final t3 = t1 + t2; // == 2.23
final t4 = t2 - t1; // == 0.23
final t5 = t1 * t2; // == 1.23;
final t6 = t1 / t2; // == 1.23
final t7 = -t1; // == -1.23

Comparision

final t1 = Fixed.from(1.23);
final t2 = Fixed.fromMinorUnits(123, scale: 2);
final t3 = Fixed.fromBigInt(BigInt.from(1234), scale: 3));

expect(t1 == t2, isTrue);
expect(t1 < t3, isTrue);
expect(t1 <= t3, isTrue);
expect(t1 > t3, isFalse);
expect(t1 >= t3, isFalse);
expect(t1 != t3, isTrue);
expect(-t1, equals(Fixed.fromMinorUnits(-123, scale: 2)));

expect(t1.isPositive, isTrue);
expect(t1.isNegative, isFalse);
expect(t1.isZero, isTrue);

expect(t1.compareTo(t2) , isTrue);

Parsing

You can parse numbers from strings:

var t1 = Fixed.parse('1.234', pattern: '#.#', scale: 2);
expect(t1.minorUnits.toInt(), equals(1234));
expect(t1.scale, equals(2));


var t1 = Fixed.parse('1,000,000.234', pattern: '#,###.#', scale: 2);
expect(t1.minorUnits.toInt(), equals(1000000.23));
expect(t1.scale, equals(2));


/// for countries that use . for thousand separators
var t1 = Fixed.parse('1.000.000,234', pattern: '#.###,#', scale: 2, invertSeparators);
expect(t1.minorUnits.toInt(), equals(1000000.23));
expect(t1.scale, equals(2));

Formating

You can also format numbers to strings

var t1 = Fixed.fromMinorUnits(1234, scale: 3);

expect(t3.toString(), equals('1.234'));

expect(t3.format('00.###0'), equals('00.12340'));

expect(t3.format('00.###0', invertSeparators: true), equals('00,12340'));

fixed's People

Contributors

bsutton avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.