FLUTTER ECOSYSTEM

a14n/dart-decimal

a14n/dart-decimal open-source repository details.

dart-decimal 프로젝트 이미지
Stars
186
Forks
54
최근 푸시(UTC)
2026. 7. 28.
프로젝트 상태
활성
Alexandre Ardhuin GitHub avatar
GITHUB User

Alexandre Ardhuin ↗

France
언어Dart

이 저장소가 배포한 패키지

사용 중인 의존성

의존성 목록 5 개
  • intl>=0.19.0 <0.21.0
  • rational^2.0.0
  • expector개발 의존성^0.1.4
  • lints개발 의존성6.0.0
  • test개발 의존성^1.25.4

원본 README

아래는 영문 원문 스냅샷입니다. 최신 내용은 GitHub에서 확인하세요.

README 펼치기 / 접기

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