Search packages, repositories and app categories

Package detailsv3.2.6

decimal

The decimal package allows you to deal with decimal numbers without losing precision.

AndroidiOSWebmacOSWindowsLinux
30-day downloads
1.1M
Likes
433
Pub points
160

Install package

flutter pub add decimal

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

The decimal package provides precise decimal arithmetic in Dart, enabling accurate handling of decimal numbers without the precision loss common in floating-point operations. It supports exact decimal representation and arithmetic, making it ideal for financial calculations and other scenarios requiring high accuracy. The package is compatible across all major platforms, including mobile, web, and desktop. It offers a clean API for creating, manipulating, and formatting decimal values with full control over rounding modes and scale.

Use cases

  • Financial calculations
  • Scientific computations
  • Currency handling
  • Precise data processing
  • Unit conversions

Key features

  • High-precision decimal arithmetic
  • No floating-point precision loss
  • Support for rounding modes
  • Cross-platform compatibility
  • Easy-to-use API

Suitable for

  • Applications requiring exact decimal math
  • Financial or accounting software
  • Systems where precision is critical
  • Developers avoiding float inaccuracies

Considerations

  • Slightly higher memory usage than doubles
  • Not optimized for performance-intensive numerical workloads
  • Requires explicit use instead of native numeric types

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

Dart Decimals

Build Status

This project enable to make computations on decimal numbers without losing precision like double operations.

For instance :

// with double
print(0.2 + 0.1); // displays 0.30000000000000004

// with decimal
print(Decimal.parse('0.2') + Decimal.parse('0.1')); // displays 0.3

Usage

To use this library in your code :

  • add a dependency in your pubspec.yaml :
dependencies:
  decimal:
  • add import in your dart code :
import 'package:decimal/decimal.dart';
  • Start computing using Decimal.parse('1.23').

Hint : To make your code shorter you can define a shortcut for Decimal.parse :

final d = (String s) => Decimal.parse(s);
d('0.2') + d('0.1'); // => 0.3

Formatting with intl

You can use the intl package to format decimal with DecimalFormat from the package:decimal/intl.dart library:

import 'package:decimal/decimal.dart';
import 'package:decimal/intl.dart';

main() {
  var value = Decimal.parse('1234.56');
  var formatter = DecimalFormatter(NumberFormat.decimalPattern('en-US'));
  print(formatter.format(value));
}

Tip: you can define an extension to make it more fluent:

extension on Decimal {
  String formatWith(NumberFormat formatter) => formatter.format(DecimalIntl(this));
}
main() {
  var value = Decimal.parse('1234.56');
  var formatter = DecimalFormatter(NumberFormat.decimalPattern('en-US'));
  print(value.formatWith(formatter));
}

WARNING: For now (2024.05.30) intl doesn't work with NumberFormat.maximumFractionDigits greater than 15 on web plateform and 18 otherwise.

Parsing with intl

You can use the NumberFormat from intl package to parse formatted decimals

var currencyFormatter = DecimalFormatter(NumberFormat.simpleCurrency(name: 'USD'));
currencyFormatter.parse('\$3.14'); // => 3.14

License

Apache 2.0

Categories

Dependencies

Dependency list 2 items

Source repository

Projects using this package

Rankings