パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v3.2.6

decimal

decimal パッケージを使用すると、精度を失うことなく小数を扱うことができます。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
105万
いいね数
433
Pub ポイント
160

パッケージをインストール

flutter pub add decimal

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

decimal パッケージは、Dart で正確な小数演算を提供し、浮動小数点演算で一般的に発生する精度の損失なしに小数を正確に扱うことを可能にします。正確な小数表現と算術をサポートしており、金融計算や高精度が必要なその他のシナリオに最適です。このパッケージは、モバイル、Web、デスクトップを含むすべての主要プラットフォームで互換性があります。丸めモードやスケールに対する完全な制御を備えた、小数値の作成、操作、フォーマット用のクリーンな API を提供しています。

用途

  • 財務計算
  • 科学計算
  • 通貨の取り扱い
  • 正確なデータ処理
  • 単位変換

主な機能

  • 高精度小数演算
  • 浮動小数点の精度損失なし
  • 丸めモードのサポート
  • クロスプラットフォーム互換性
  • 使いやすい API

適している対象

  • 正確な10進数の数学演算を必要とするアプリケーション
  • 財務または会計ソフトウェア
  • 正確性が重要なシステム
  • 開発者が浮動小数点の不正確さを回避しています

注意事項

  • ダブルよりもわずかに高いメモリ使用量
  • パフォーマンス重視の数値ワークロードに最適化されていません
  • ネイティブな数値型ではなく、明示的な使用が必要です

元の README

関連するソースリポジトリの 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

カテゴリー

依存関係

依存関係一覧 2 件

ソースリポジトリ

このパッケージを使用しているプロジェクト

ランキング