共计 3211 个字符,预计需要花费 9 分钟才能阅读完成。
string_num_calculate
一个用于计算数字字符串算式的库。
Calculate mathematical formula result from string.
可不便用于 app 制作虚构键盘,虚构计算器。
With this package, You can easily make virtual keyboard or virtual calculator for your APP.
已通过测试,可放心使用。
All tests passed, enjoy :)
API
Calculate
// Main Class
- static Map<String, Function> funs
定义的办法映射
- static List<String> toNumberList
把字符串中的数字和算式符号提取为 list
Extract numbers and operators from the string into a list
- static List<String> formatList
把数字和算式符号列表中反复的符号去除
Remove redundant operators from list
- static num LinierCalculate
间接计算字符串式算式
Calculate directly from string
- static num ListCalculate
计算 list 式算式
Calculate for list
- static num plus
加法
plus
- static num substract
减法
minus
-
static num multiply
乘法
multiply
- static num divide
除法
divide
- static num percent
生产或者百分式算法
percentage
装置 Installation
在 pubspec.ymal 文件中退出,而后运行 pub get packages
命令。
Add this line into pubspec.ymal
, then run pub get packages
dependencies:
string_num_calculate: ^0.0.1
用法说明 Usage
import 'package:string_num_calculate/string_num_calculate.dart';// 引入
Calculate.plus(1, 2);//=>3
Calculate.substract(1, 2);//=>-1
Calculate.multiply(1, 2);//=>2
Calculate.divide(1, 2);//=>.5
Calculate.percent(1, 2);//=>0.02
Calculate.ListCalculate(['1', '-', '5']);//=>-4
Calculate.toNumberList('1-5');//=>['1','-','5']
Calculate.formatList(['1', '+', '-', '5']);//=>['1','-','5']
Calculate.LinierCalculate('1+3+5');//=>9
测试
// 加法计算 Plus
test('static [plus]计算加', () {expect(Calculate.plus(1, 2), 3);
expect(Calculate.plus(1, -2), -1);
expect(Calculate.plus(-1, -2), -3);
expect(Calculate.plus(-.1, -2), -2.1);
expect(Calculate.plus(-.1, -0.2), -0.30000000000000004);
});
// 减法计算 Minus
test('static [substract]计算减', () {expect(Calculate.substract(1, 2), -1);
expect(Calculate.substract(.1, -2), 2.1);
expect(Calculate.substract(0.1, -2), 2.1);
});
// 乘法计算 Multiply
test('static [multiply]计算乘', () {expect(Calculate.multiply(1, 2), 2);
expect(Calculate.multiply(1, -2), -2);
expect(Calculate.multiply(1, 0), 0);
});
// 除法计算 Divide
test('static [divide]计算除', () {expect(Calculate.divide(1, 2), .5);
expect(Calculate.divide(1, -2), -.5);
});
// 小费计算 Tips
test('static [percent]计算百分数', () {expect(Calculate.percent(1, 2), 0.02);
expect(Calculate.percent(1, -2), -0.02);
expect(Calculate.percent(100, 2), 2);
});
// 数字 list 计算 Calculate for list
test('static [ListCalculate]数字 list 计算', () {expect(Calculate.ListCalculate(['1', '-', '5']), -4);
expect(Calculate.ListCalculate(['1', '+', '5']), 6);
expect(Calculate.ListCalculate(['-1', '×', '5']), -5);
expect(Calculate.ListCalculate(['1', '%', '5']), 0.05);
});
// 数字字符串转换成数字 list Convert from string to list
test('static [toNumberList]数字字符串转 list', () {expect(Calculate.toNumberList('1-5'), ['1', '-', '5']);
expect(Calculate.toNumberList('-1+5'), ['-','1', '+', '5']);
expect(Calculate.toNumberList('1×5'), ['1', '×', '5']);
expect(Calculate.toNumberList('1%5'), ['1', '%', '5']);
});
// 数字 list 去除符号雷同 Remove redundant operators from list
test('static [formatList]数字字符串转 list', () {expect(Calculate.formatList(['1', '+', '-', '5']), ['1', '-', '5']);
expect(Calculate.formatList(['1', '+', '+', '5']), ['1', '+', '5']);
expect(Calculate.formatList(['1', '+', '×', '5']), ['1', '×', '5']);
expect(Calculate.formatList(['1', '+', '%', '5']), ['1', '%', '5']);
});
// 字符串计算 Calculate from string
test('Calculate->[LinierCalculate]计算一个字符串计算', () {expect(Calculate.LinierCalculate('1+.3+5'), 6.3);
expect(Calculate.LinierCalculate('-1+0.3+5'), 4.3);
expect(Calculate.LinierCalculate('-1*3+5'), 2);
expect(Calculate.LinierCalculate('-1/2+5'), 4.5);
});
正文完