搜索插件、代码仓与 APP 分类

插件详情v1.0.5

num_utilities

一组用于数字、整数和双精度浮点数的扩展方法;以及数字、整数和双精度浮点数的可迭代集合。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
3240
喜欢数
2
Pub 分数
150

安装插件

flutter pub add num_utilities

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

num_utilities 是一个 Dart 包,通过有用的扩展方法增强了数值类型(int、double)及其可迭代对象。它提供了便捷的数学运算、转换和数据操作工具,提升了代码可读性并减少了样板代码。该包轻量级,旨在无缝集成到所有主要平台上的 Flutter 和 Dart 项目中。非常适合需要进行计算或统计处理的应用程序中的数值数据开发。

适合场景

  • 对数字列表执行批量数学运算
  • 简化数值类型转换
  • 增强算术表达式的可读性
  • 处理数值计算中的边缘情况
  • 在集合上实现统计函数
  • 简化数据预处理

主要特点

  • int 和 double 的扩展方法
  • 数值集合的 Iterable 支持
  • 简洁、可链式调用的语法
  • 跨平台兼容性
  • 最小依赖
  • 通过 pub add 轻松集成

适合对象

  • 需要数学辅助工具的 Flutter 开发人员
  • 移动和Web应用中的数据处理
  • 涉及数值分析的项目
  • 注重代码清晰性和可读性的团队
  • 教育类或原型应用

使用建议

  • 仅限数值类型
  • 没有针对极端值的内置错误处理
  • 未针对大规模数值计算进行优化
  • 需要手动测试边缘情况
  • 不替代完整的数学库,例如 'math_expressions'
  • 最适合简单的到中等的数值任务

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

num_utilities

A collection of extension methods for [num]s, [int]s, and [double]s; and iterables of [num]s, [int]s, and [double]s.

See: list_utilities

Usage

import 'package:num_utilities/num_utilities.dart';

Numbers

[num]s, [int]s, and [double]s were extended with the following methods/getters:

isPositive

[isPositive] returns true if the number's sign is positive.

final numA = 5;
print(numA.isPositive); // true

final numB = -5;
print(numB.isPositive); // false
equals

[equals] is equivalent to the == operator, but has an optional parameter to round the numbers before comparing them, by calling [roundTo] (see below.)

final numA = 5.556;
final numB = 5.557;
print(numA.equals(numB)); // false
print(numA.equals(numB, 100)); // true
isWithin

[isWithin] returns true if the number matches or is within the range of any of the parts defined in the provided [expression].

The provided [expression] must be a string of numbers and/or ranges separated by commas.

final value = 7;
print(value.isWithin('0,2,4,6')); // false
print(value.isWithin('1,2,6-8,10')); // true
print(value.isWithin('0-4,7,12-15')); // true
isWithinRange

[isWithinRange] returns true if this number is within the range of the provided [start] and [end] values.

final value = 7;
print(value.isWithinRange(4, 8)); // true
print(value.isWithinRange(8, 4)); // true
print(value.isWithinRange(2, 6)); // false
roundTo

[roundTo] rounds the number by the value: (number * value).round() / value

final number = 5.556;
print(number.roundTo(10)); // 5.6
roundToPrecision

[roundToPrecision] rounds the number to the specified number of decimal points.

final number = 5.5567893;
print(number.roundToPrecision(3)); // 5.557

Iterables

Iterables of [num]s, [int]s, and [double]s were extended with the following methods:

sum

The [sum] method returns the sum of all of the values in the iterable.

final list = [1, 2, 3, 4, 5];
print(list.sum()); // 15
absSum

The [absSum] method returns the sum of all of the absolute values in the iterable.

[absSum] has an optional parameter to invert the returned value.

final list = [1, -2, 3, -4, 5];
print(list.absSum()); // 15
print(list.absSum(false)); // 15
print(list.absSum(true)); // -15
print(list.sum()); // 3
highest

The [highest] getter returns the highest value in the iterable.

final list = [1, 2, 3, 4, 5];
print(list.highest); // 5
lowest

The [lowest] getter returns the lowest value in the iterable.

final list = [1, 2, 3, 4, 5];
print(list.lowest); // 1

Range

The [Range] object defines a range of numbers between specified [min] and [max] values, and has convienence methods for generating numbers within the range and for interpolating values within the range.

final range = Range(5, 15);
print(range.min); // 5
print(range.max); // 15

span

The [span] getter returns the difference between the [min] and [max] values.

print(range.span); // 10

random

The [random] method will output a number between the [min] value, inclusive, and the [max] value, exclusive.

[random] accepts an optional [seed] value.

print(range.random());
print(range.random(seed));

secureRandom

The [secureRandom] method will output a cryptographically secure number between the [min] value, inclusive, and [max] value, exclusive.

print(range.secureRandom());

interpolate

The [interpolate] method returns a [List] of numbers spanning a defined number of [steps] between the [min] and [max] values.

print(range.interpolate(5)); // [5.0, 7.0, 9.0, 11.0, 13.0]
inclusive

[interpolate]'s inclusive parameter can be used to include the [max] number within the output [List].

print(range.interpolate(5, inclusive: true)); // [5.0, 7.0, 9.0, 11.0, 13.0, 15.0]
randomize

[interpolate]'s randomize parameter can be used to apply randomness to the output numbers, where a value of 0.0 doesn't apply any randomness to the numbers, while a value of 1.0 will randomize the output numbers within the entire range of the radius around each step.

print(range.interpolate(5, randomize: 1.0)); // [5.23, 6.18, 8.27, 10.27, 13.46]

所属分类

源码仓库

使用此插件的项目

暂无相关数据。