Flutter高精度数值处理插件sio_big_decimal的使用
Flutter高精度数值处理插件sio_big_decimal的使用
安装
安装插件:
dart pub add sio_big_decimal
导入插件:
import 'package:sio_big_decimal/sio_big_decimal.dart';
使用示例
以下是一个完整的示例,展示了如何使用 sio_big_decimal
插件进行高精度数值处理。
import 'package:sio_big_decimal/sio_big_decimal.dart';
void main() {
// 从BigInt创建BigDecimal
print(BigDecimal.fromBigInt(BigInt.from(12345))); // 12345
print(BigDecimal.fromBigInt(BigInt.from(-12345))); // -12345
print(BigDecimal.fromBigInt(BigInt.from(12345), precision: 1)); // 1234.5
print(BigDecimal.fromBigInt(BigInt.from(12345), precision: 4)); // 1.2345
print(BigDecimal.fromBigInt(BigInt.from(12345), precision: 8)); // 0.00012345
print(BigDecimal.fromBigInt(BigInt.from(0), precision: 8)); // 0.00000000
print(BigDecimal.fromBigInt(
BigInt.parse(
'1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890'),
precision: 90)); // very high precision check
try {
print(BigDecimal.fromBigInt(BigInt.from(12345), precision: -1));
} catch (e) {
print(e);
}
print('==================================================');
// 解析字符串为BigDecimal
print(BigDecimal.parse('12345')); // 12345
print(BigDecimal.parse('-12345'));
print(BigDecimal.parse('12345', precision: 2)); // 12345.00
print(BigDecimal.parse('123.45')); // 123
print(BigDecimal.parse('123.67')); // 124
print(BigDecimal.parse('123.45', precision: 4)); // 123.4500
print(BigDecimal.parse('0', precision: 8)); // 0.00000000
print(BigDecimal.parse('0.001', precision: 4)); // 0.0010
try {
print(BigDecimal.parse('12345', precision: -1));
} catch (e) {
print(e);
}
print('==================================================');
// 从double创建BigDecimal
print(BigDecimal.fromDouble(12345)); // 12345
print(BigDecimal.fromDouble(123.45)); // 123.45
print(BigDecimal.fromDouble(-123.45));
print(BigDecimal.fromDouble(123.4500)); // 123.45
print(BigDecimal.fromDouble(0)); // 0
print(BigDecimal.fromDouble(0, precision: 8)); // 0.00000000
print(BigDecimal.fromDouble(123.450, precision: 5)); // 123.45000
print(BigDecimal.fromDouble(123.45678, precision: 2)); // 123.46
try {
print(BigDecimal.fromDouble(123.45, precision: -1));
} catch (e) {
print(e);
}
print('==================================================');
// 创建BigDecimal零值
print(BigDecimal.zero()); // 0
print(BigDecimal.zero(precision: 8)); // 0.00000000
print(BigDecimal.zero(precision: -1)); // 0
print('==================================================');
// 进行加减乘除运算
final x = BigDecimal.parse('1.222', precision: 3);
final y = BigDecimal.parse('0.2225', precision: 4);
final z = BigDecimal.parse('1.888', precision: 4);
final zero = BigDecimal.zero();
final addition = x + z; // 3.11 (precision 2)
final subtract = x - y; // 0.9995 (precision 4)
final multiplication = x * z; // 2.307136 (precision: 6)
final division = x / y; // 5.492 (precision: 3)
// 输出结果
print('$x + $z = $addition (precision: ${addition.precision})'); // 1.222 + 1.8880 = 3.11 (precision: 2)
print('$x - $y = $subtract (precision: ${subtract.precision})'); // 1.222 - 0.2225 = 0.9995 (precision: 4)
print('$x * $z = $multiplication (precision: ${multiplication.precision})'); // 1.222 * 1.8880 = 2.307136 (precision: 6)
print('$x / $y = $division (precision: ${division.precision})'); // 1.222 / 0.2225 = 5.492 (precision: 3)
try {
print(x / zero);
} catch (e) {
print(e);
}
print('==================================================');
// 使用静态方法进行加减乘除运算
final add = BigDecimal.add(x, z, precision: 8); // 3.11000000 (precision 8)
final sub = BigDecimal.subtract(x, y, precision: 8); // 0.99950000 (precision 8)
final multiply = BigDecimal.multiply(x, z, precision: 8); // 2.30713600 (precision: 8)
final div = BigDecimal.divide(x, y, precision: 19); // 5.4921348314606741573 (precision: 19)
print(
'BigDecimal.add($x, $z, precision: 8) = $add (precision: ${add.precision})');
print(
'BigDecimal.subtract($x, $y, precision: 8) = $sub (precision: ${sub.precision})');
print(
'BigDecimal.multiply($x, $z, precision: 8) = $multiply (precision: ${multiply.precision})');
print(
'BigDecimal.divide($x, $y, precision: ${div.precision}) = $div (precision: ${div.precision})');
try {
print(BigDecimal.divide(x, zero));
} catch (e) {
print(e);
}
print('==================================================');
// 清零操作
final testNotZero = BigDecimal.parse('-12.4433', precision: 4);
print(testNotZero);
final testZero = testNotZero.clear();
print(testZero);
print('==================================================');
}
更多关于Flutter高精度数值处理插件sio_big_decimal的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter高精度数值处理插件sio_big_decimal的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是关于如何在Flutter项目中使用sio_big_decimal
插件进行高精度数值处理的代码案例。sio_big_decimal
是一个用于Flutter和Dart的高精度数值处理库,特别适用于财务和科学计算等需要高精度计算的场景。
步骤 1: 添加依赖
首先,你需要在pubspec.yaml
文件中添加sio_big_decimal
依赖:
dependencies:
flutter:
sdk: flutter
sio_big_decimal: ^x.y.z # 替换为最新版本号
然后运行flutter pub get
来获取依赖。
步骤 2: 导入库
在你的Dart文件中导入sio_big_decimal
库:
import 'package:sio_big_decimal/sio_big_decimal.dart';
步骤 3: 使用BigDecimal
进行高精度数值处理
以下是一些使用BigDecimal
进行高精度数值处理的示例代码:
示例 1: 基本运算
void main() {
// 创建 BigDecimal 实例
BigDecimal num1 = BigDecimal.fromString('12345678901234567890.1234567890');
BigDecimal num2 = BigDecimal.fromString('98765432109876543210.9876543210');
// 加法
BigDecimal sum = num1 + num2;
print('Sum: $sum');
// 减法
BigDecimal difference = num1 - num2;
print('Difference: $difference');
// 乘法
BigDecimal product = num1 * num2;
print('Product: $product');
// 除法,注意设置精度和舍入模式
BigDecimal quotient = num1.divide(num2, 20, RoundingMode.HALF_UP);
print('Quotient: $quotient');
}
示例 2: 比较数值
void compareNumbers() {
BigDecimal num1 = BigDecimal.fromString('12345.6789');
BigDecimal num2 = BigDecimal.fromString('12345.6788');
if (num1.greaterThan(num2)) {
print('num1 is greater than num2');
} else if (num1.lessThan(num2)) {
print('num1 is less than num2');
} else {
print('num1 is equal to num2');
}
}
示例 3: 设置精度和舍入模式
void setPrecisionAndRounding() {
BigDecimal num = BigDecimal.fromString('12345.6789123456789');
// 设置精度为5,舍入模式为HALF_UP
BigDecimal roundedNum = num.setScale(5, RoundingMode.HALF_UP);
print('Rounded Number: $roundedNum');
}
注意事项
- 精度和舍入模式:在进行除法运算时,需要指定精度和舍入模式,否则可能会抛出异常。
- 性能:高精度数值运算通常比基本数值类型运算要慢,因此在实际应用中需要权衡精度和性能。
通过上述代码示例,你可以在Flutter项目中使用sio_big_decimal
库进行高精度数值处理。希望这对你有所帮助!