Skip to content

Commit

Permalink
Merge pull request #8 from krissss/7-bccomp
Browse files Browse the repository at this point in the history
compare 支持科学计数法
  • Loading branch information
krissss authored Jun 25, 2024
2 parents ba636e9 + 0175c23 commit 1c625b4
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/BC.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @method float|string div(...$numbers)
* @method float|string mod(...$numbers)
* @method float|string pow(...$numbers)
* @method int compare($number1, $number2)
*/
class BC extends BaseBC
{
Expand All @@ -24,8 +25,12 @@ public function __call($name, $arguments)
}
$bcs = BCS::create($arguments[0], $this->config);
unset($arguments[0]);
/** @var BCS $bcs */
$bcs = $bcs->$name(...$arguments);
if ($name === 'compare') {
/** @var int $bcs */
return $bcs;
}
/** @var BCS $bcs */
return $bcs->getResult();
}
}
16 changes: 13 additions & 3 deletions src/BCS.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class BCS extends BaseBC
*/
public function __construct($number, $config = [])
{
$this->result = $number;
parent::__construct($config);
$this->result = $this->numberFormat($number);
}

/**
Expand All @@ -48,7 +48,7 @@ public function __call($name, $arguments)
throw new \Exception("{$bcName} not in ::bcEnables");
}
foreach ($arguments as $number) {
$number = number_format($number, $this->config['operateScale']+1, '.', '');
$number = $this->numberFormat($number);
if ($bcName === 'bcpow') {
$number = intval($number);
}
Expand Down Expand Up @@ -86,7 +86,7 @@ public function getSqrt()
*/
public function compare($number)
{
return bccomp($this->result, $number, $this->config['scale']);
return bccomp($this->result, $this->numberFormat($number), $this->config['scale']);
}

/**
Expand Down Expand Up @@ -118,4 +118,14 @@ public function isLargerThan($number)
{
return $this->compare($number) === 1;
}

/**
* 格式化数字
* @param $number
* @return string
*/
private function numberFormat($number): string
{
return number_format($number, $this->config['operateScale']+1, '.', '');
}
}
2 changes: 1 addition & 1 deletion tests/BCParserTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php


use kriss\bcmath\BCParser;

test('parse', function () {
// 正常
$result = BCParser::create(['scale' => 2])->parse('1.2*1.3+5');
Expand Down
1 change: 1 addition & 0 deletions tests/BCSTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use kriss\bcmath\BCS;

test('get result', function () {
// 默认四舍五入
$result = BCS::create(1.5, ['scale' => 2])->add(1.2)->mul(2)->sub(1.5)->getResult();
Expand Down
2 changes: 1 addition & 1 deletion tests/BCSummaryTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php


use kriss\bcmath\BCSummary;

test('average', function () {
// 小数
$result = BCSummary::create(['scale' => 3])->average(18.8, [2.2, 2.2, 4.4]);
Expand Down
14 changes: 13 additions & 1 deletion tests/BCTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php


use kriss\bcmath\BC;

test('add', function () {
$result = BC::create(['scale' => 2])->add(1.2, 1.3);
expect($result)->toEqual(2.5);
Expand Down Expand Up @@ -34,3 +34,15 @@
$result = BC::create(['scale' => 6])->add();
expect($result)->toEqual(0);
});
test('compare', function () {
$result = BC::create(['scale' => 6])->compare(1, 2);
expect($result)->toEqual(-1);
$result = BC::create(['scale' => 6])->compare(1, 1);
expect($result)->toEqual(0);
$result = BC::create(['scale' => 6])->compare(2, 1);
expect($result)->toEqual(1);
$result = BC::create(['scale' => 6])->compare(2, 1);
expect($result)->toEqual(1);
$result = BC::create(['scale' => 2])->compare(1000.00008, 1000);
expect($result)->toEqual(0);
});
27 changes: 26 additions & 1 deletion tests/IssueTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
<?php

use kriss\bcmath\BC;
use kriss\bcmath\BCS;

test('issues2', function () {
$total = BCS::create(0, ['scale' => 6]);
$total->add(1);
$total->add(0.000008);

expect($total->getResult())->toEqual(1.000008);

$total = BCS::create(0, ['scale' => 6]);
$total->add(1e3);
$total->add(0.000008);
expect($total->getResult())->toEqual(1000.000008);

$total = BCS::create(0, ['scale' => 6]);
$total->add('1e3');
$total->add(0.000008);
expect($total->getResult())->toEqual(1000.000008);
});

test('issues7', function () {
$result = BC::create(['scale' => 2])->compare('1e3', 100);
expect($result)->toEqual(1);

$result = BCS::create(1233123131432143214321412.123)->compare(12312312312312);
expect($result)->toEqual(1);

$result = BCS::create('1e3')->compare(12312);
expect($result)->toEqual(-1);

$result = BCS::create(123)->compare('1e3');
expect($result)->toEqual(-1);
});

0 comments on commit 1c625b4

Please sign in to comment.