yudhaang / equation
dev-master
2022-09-30 11:25 UTC
Requires
- php: >=7.4
This package is auto-updated.
Last update: 2024-09-29 06:09:12 UTC
README
Equation是一个PHP库,用于通过单位转换梯子转换数量。
目录
介绍
在开发记录商品和原料数量的应用程序时,我们经常面临相同商品的不同单位数量的计算问题。例如交易
- 购买1升
Minyak
- 生产一个产品使用
Minyak
100毫升
根据上述交易数据,我们经常需要制作Minyak
的剩余库存报表。为了创建有效数据,需要使用能够帮助简化不同单位数量转换过程的库。Equation
旨在成为单位转换工具,以帮助软件开发者更容易地开发应用程序。祝您使用愉快,希望有所帮助。
入门
需求
- PHP版本 >= 7.4
- Composer
项目结构
Equation
├── src
│ ├── Data
│ │ └── DefaultUnit.php
│ ├── Example
│ │ ├── data.json
│ │ └── index.php
│ └── Equation.php
├── .gitignore
├── composer.json
├── LICENSE
└── README.md
安装
通过命令行运行composer。查看composer 文档。
composer require yudhaang/equation
基本用法
使用类
将类包含到您的PHP文件中,并初始化为变量。
- 在您的项目中打开文件:
示例:path_of_your_project/index.php
- 写入以下代码以包含类
<?php require __DIR__ . '/vendor/autoload.php'; use yudhaAng/Equation;
- 初始化对象变量
$units_data = [ ['name'=>'kg', 'qty' => 1], ['name'=>'hg', 'qty' => 10], ['name'=>'dag', 'qty' => 10], ['name'=>'g', 'qty' => 10], ['name'=>'dg', 'qty' => 10], ['name'=>'mg', 'qty' => 10] ]; $options = [ 'units' => $units_data ]; $equation = new Equation($options);
函数和方法
- 转换
$equation = new Equation($options); $qty = 1.745; $from_unit = 'kg'; $to_unit = 'g'; $output = $equation->convert($qty, $from_unit, $to_unit); // Output Number = 1745 $qty = 1245; $from_unit = 'g'; $to_unit = 'dag'; $output = $equation->convert($qty, $from_unit, $to_unit); // Output Number = 124.5
- 渲染文本
$equation = new Equation($options); $qty = 1.745; $unit = 'kg'; $output = $equation->renderText($qty, $unit); // Output String = 1 kg, 7 hg, 4 dag, 5 g
- 转换文本
$equation = new Equation($options); // Unspecific unit output $text = '1 kg, 7 hg, 4 dag, 5 g'; $output = $equation->converText($text); // Output Array = ['kg'=>1.745, 'hg'=>17.45, 'dag'=>174.5, 'g'=>1745, 'dg'=>17450, 'cg'=>174500, 'mg'=>1745000] // Specific unit output $text = '1 kg, 7 hg, 4 dag, 5 g'; $unit = 'g'; $output = $equation->converText($text, $unit); // Output Number = 1745
高级用法
- 自定义单位字段选项。
$units_data = [ ['text'=>'kg', 'value' => 1], ['text'=>'hg', 'value' => 10], ['text'=>'dag', 'value' => 10], ['text'=>'g', 'value' => 10], ['text'=>'dg', 'value' => 10], ['text'=>'mg', 'value' => 10] ]; $options = [ 'units' => $units_data, 'unit_property' => 'text', 'qty_property' => 'value' ]; $equation = new Equation($options); $output = $equation->convert(1245,'g','dag'); // Output Number = 1745
- 更改单位转换梯子。
$units = [ ['text'=>'l', 'value' => 1], ['text'=>'ml', 'value' => 1000] ]; $equation->units($units); $output = $equation->convert(1,'l','ml'); // Output Number = 1000
作者
Yudha Angga Wijaya
许可证
Equation
是开源软件,以MIT许可证授权。