搜尋套件、儲存庫與 APP 分類

套件詳情v3.2.6

decimal

decimal 套件可讓您處理十進位數字,而不會遺失精確度。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
105萬
喜歡數
433
Pub 分數
160

安裝套件

flutter pub add decimal

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

decimal 套件在 Dart 中提供精確的十進位算術,可準確處理十進位數字,避免浮點運算常見的精度損失。它支援精確的十進位表示與算術運算,非常適合用於財務計算及其他需要高準確度的場景。此套件相容於所有主要平台,包含行動裝置、網頁與桌面端。它提供一個乾淨的 API,可用於建立、操作與格式化十進位值,並完全控制四捨五入模式與尺度。

適用情境

  • 財務計算
  • 科學計算
  • 貨幣處理
  • 精確的數據處理
  • 單位轉換

主要特色

  • 高精度十進位算術
  • 無浮點精度損失
  • 對捨入模式的支援
  • 跨平台相容性
  • 易於使用的 API

適合對象

  • 需要精確十進制數學運算的應用程式
  • 財務或會計軟體
  • 對精度要求嚴格的情況
  • 開發人員避免浮點精度問題

使用建議

  • 比雙精度浮點數略高的記憶體使用量
  • 未針對效能密集型數值工作負載進行優化
  • 需要顯式使用,而不是原生數值類型

原始 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 項

原始碼儲存庫

使用此套件的專案

所在榜單