定制 mezinn/number 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mezinn/number

最新稳定版本:0.1.0

Composer 安装命令:

composer require mezinn/number

包简介

A library for working with numbers in PHP

README 文档

README

Disclaimer: This component is intended as an example and is not meant to be used in production. It is provided as a learning tool and should be used at your own risk.

Number is a library for working with numbers in PHP. It provides a Number class that represents a decimal number and a NumberCalculator class that performs arithmetic operations on two numbers. Additionally, it has a NumberFactory class that creates a Number instance from a string value and a NumberFormatter class that formats a Number instance as a string with thousands separator and optional sign.

Installation

Install the library via Composer:

composer require mezinn/number

Usage

Creating a Number instance

To create a Number instance, use the NumberFactory class as follows:

use mezinn\number\NumberFactory;

$numberFactory = new NumberFactory();
$number = $numberFactory->create('123.45');

Performing arithmetic operations

To perform arithmetic operations on two Number instances, use the NumberCalculator class as follows:

use mezinn\number\Number;
use mezinn\number\NumberCalculator;
use mezinn\number\NumberFactory;

$numberFactory = new NumberFactory();
$numberCalculator = new NumberCalculator($numberFactory);

$a = $numberFactory->create('123.45');
$b = $numberFactory->create('67.89');
$c = $numberFactory->create('2');

$result = $numberCalculator->add($a, $b);
// $result is a Number instance representing the value 191.34

$result = $numberCalculator->subtract($a, $b);
// $result is a Number instance representing the value 55.56

$result = $numberCalculator->multiply($a, $b);
// $result is a Number instance representing the value 8381.02

$result = $numberCalculator->divide($a, $b);
// $result is a Number instance representing the value 1.81

$result = $numberCalculator->pow($a, $c);
// $result is a Number instance representing the value 15239.90

$result = $numberCalculator->sqrt($a);
// $result is a Number instance representing the value 11.11

Formatting a Number instance

To format a Number instance as a string with thousands separator and optional sign, use the NumberFormatter class as follows:

use mezinn\number\Number;
use mezinn\number\NumberFactory;
use mezinn\number\NumberFormatter;

$numberFactory = new NumberFactory();
$numberFormatter = new NumberFormatter();

$number = $numberFactory->create('-1234567.89');
$formatted = $numberFormatter->format($number, ',', true);
// $formatted is '-1,234,567.89'

License

Number is released under the MIT License.

统计信息

  • 总下载量: 7
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-26