decimal
decimal 套件可讓您處理十進位數字,而不會遺失精確度。
a14n/dart-decimal open-source repository details.
以下為英文專案原文快照,最新內容請造訪 GitHub。
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
To use this library in your code :
pubspec.yaml :dependencies:
decimal:
dart code :import 'package:decimal/decimal.dart';
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
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.
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
Apache 2.0
先依使用情境篩選,再比較專案活躍情況,最後到原始儲存庫確認使用要求。
“A repository is the most basic element of GitHub.”
GitHub Docs — About repositories ↗