A dart implementation of fixed precision numbers.

Related tags

Templates fixed
Overview

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'));
Comments
  • migrate to decimal package

    migrate to decimal package

    Following up to https://github.com/noojee/money.dart/issues/54#issuecomment-970085733 here's an attemp to use the decimal package.

    (IMHO this package should be merged into money2 directly and remained an implementation detail of money2 inside src/)

    opened by a14n 7
  • tryParse with scale does not round the value.

    tryParse with scale does not round the value.

    This test is failing.

    Fixed gives '3.1415' which is wrong.

    // Failing test expect(Fixed.tryParse('3.1415926535897932', scale: 4).toString(), '3.1416');

    opened by oakstair 3
  • MIT or Proprietary?

    MIT or Proprietary?

    The license distributed with this package is MIT.

    However, every file has a contradictory notice stating:

    • Unauthorized copying of this file, via any medium is strictly prohibited
    • Proprietary and confidential

    This effectively renders the package completely unusable as the very act of installing this package from pub.dev means you're copying the files. The statements are in complete contradiction of the MIT open source license.

    So which is it? I'm confused.

    opened by jocubeit 2
  • Minimum Dart version is too high

    Minimum Dart version is too high

    The minimum Dart version supported by this package is 2.14.4. Does the package use any language features that are not supported in earlier versions? If not, v2.12.0 would be ideal as any version before it doesn't support null safety.

    https://github.com/bsutton/fixed/blob/main/pubspec.yaml#L7

    opened by britannio 2
  • IntegerDivisionByZeroException when converting toInt from zero with default scale

    IntegerDivisionByZeroException when converting toInt from zero with default scale

    import 'package:fixed/fixed.dart';

    void main() { Fixed zero = Fixed.fromInt(0); print(zero.toString()); zero.toInt(); }

    /Users//fvm/versions/stable/bin/cache/dart-sdk/bin/dart --enable-asserts /Users//StudioProjects/test/lib/main.dart 0.00 Unhandled exception: IntegerDivisionByZeroException #0 _BigIntImpl.~/ (dart:core-patch/bigint_patch.dart:1702:7) #1 Fixed.toInt (package:fixed/src/fixed.dart:475:30) #2 main (package:test_project/main.dart:7:8) #3 _delayEntrypointInvocation. (dart:isolate-patch/isolate_patch.dart:297:19) #4 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)

    Process finished with exit code 255

    opened by dmitry-kotorov 1
Owner
Brett Sutton
Long time hacker with a background in C/C++/Java. I'm currently sampling the delights of dart and flutter.
Brett Sutton
A flutter widget that animates scrolling through a set of fixed size containers.

Spinner This flutter package implements a simple Spinner animation that cycles through any number of fixed size child widgets. Useful for selecting a

Mark Schmidt 6 Aug 3, 2021
This is a dart package that converts words to numbers. It can be used in Flutter and normal Dart programs

Wordstonumbers.dart Wordstonumbers.dart is a simple dart package that converts a string of simple worded numbers into digits (e.g one hundred -> 100).

Michael Essiet 3 Oct 17, 2022
A simple dart package to convert large numbers to a human readable format. 1278 to 1.2K instead, for example.

A simple dart package to convert large numbers to a human readable format. 1278 to 1.2K instead, for example. Features Represents large numbers in ter

Rohit V 1 Oct 8, 2022
A Mobile application developed with Flutter and Dart to do math operations with binary numbers.

Math operations with Binary Numbers Readme PT About this Project Mobile application developed with Flutter and Dart to do math operations as sum, subt

Manoel Ribeiro 3 Nov 3, 2020
A Dart widget for entering international telephone numbers with dropdown searching input countries

Dart Tel Input A Dart widget for entering international telephone numbers with dropdown searching input countries Getting Started Add the following li

աɨռɢӄաօռɢ 8 Oct 29, 2020
A Dart package that converts big numbers to what's pleasant to see

Convert big numbers to what's pleasant to see (an adorable, little girl, perhaps?)

Nobuharu Shimazu 3 Apr 16, 2022
A library for parsing and encoding IEEE-754 binary floating point numbers.

Dart IEEE754 library This library provides decoding and transforming IEEE754 floating point numbers in binary format, double format, or as exponent an

null 0 Dec 24, 2021
Leverages libphonenumber to allow for asynchronous and synchronous formatting of phone numbers in Flutter apps

Leverages libphonenumber to allow for asynchronous and synchronous formatting of phone numbers in Flutter apps. Includes a TextInputFormatter to allow real-time AsYouType formatting.

Bottlepay 43 Nov 2, 2022
Numbers is simple game to improve problem solving skills and it is built in Flutter Framework

Numbers - Flutter Game Numbers is a simple game built in Flutter Framework and is purely based on numbers to improve problem solving skills. Screensho

Thamaraiselvam 49 Oct 21, 2022
Attendancelist - App to add Students attendance based on roll numbers and calculate percentage.

attedance A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started if thi

null 0 Jan 1, 2022
Find underused colors, overused magical numbers and the largest classes in any Flutter project.

Flutter Resource Ranker It is easy to overuse colors, write magical numbers or long classes. This project has a script to help you detect these. This

Bernardo Ferrari 24 Jul 12, 2021
Package provides light widgets [for Linkify, Clean] and extensions for strings that contain bad words/URLs/links/emails/phone numbers

Package provides light widgets [for Linkify, Clean] and extensions for strings that contain bad words/URLs/links/emails/phone numbers

BetterX.io 4 Oct 2, 2022
Get It - Simple direct Service Locator that allows to decouple the interface from a concrete implementation and to access the concrete implementation from everywhere in your App. Maintainer: @escamoteur

❤️ Sponsor get_it This is a simple Service Locator for Dart and Flutter projects with some additional goodies highly inspired by Splat. It can be used

Flutter Community 1k Jan 1, 2023
Socketio dart server and client - Full Socket.io implementation using Dart Lang

Getting Started Step 1: Run dart_server.dart Step 2: Android Emulator has proble

Trần Thiên Trọng 1 Jan 23, 2022
The Dart language implementation of gRPC.

The Dart implementation of gRPC: A high performance, open source, general RPC framework that puts mobile and HTTP/2 first. Learn more Quick Start - ge

grpc 737 Jan 6, 2023
A simple dart zeromq implementation/wrapper around the libzmq C++ library

dartzmq A simple dart zeromq implementation/wrapper around the libzmq C++ library Features Currently supported: Creating sockets (pair, pub, sub, req,

Moritz Wirger 18 Dec 29, 2022
Firebase dart common interface and implementation for Browser, VM, node and flutter

firebase.dart Firebase dart common interface and implementation for Browser, VM, node and flutter Firebase Initialization Usage in browser import 'pac

Tekartik 6 Nov 28, 2021
The reference implementation of Sass, written in Dart.

A Dart implementation of Sass. Sass makes CSS fun again. Using Dart Sass From Chocolatey or Scoop (Windows) From Homebrew (macOS) Standalone From npm

Sass 3.4k Jan 5, 2023
A web-safe implementation of dart.io.Platforms. Helps avoid the "Unsupported operation: Platform._operatingSystem" runtime error.

Universal Platform - A Web-safe Platform class Currently, if you include the dart.io.Platform anywhere in your code, your app will throw the following

gskinner team 86 Nov 20, 2022