Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Satini_pvduc
/
daito-utils
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Authored by
RESAZIP-PC\resaz
2026-02-22 00:03:44 +0700
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Commit
cc727ce27b8d21a700e95e22f51814dc750b5860
cc727ce2
1 parent
22571a34
add daitomath
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
148 additions
and
1 deletions
README.md
composer.json
composer.lock
src/DaitoMath.php
README.md
View file @
cc727ce
...
...
@@ -50,3 +50,43 @@ composer require daito/lib:^1.0
# or
composer update daito/lib
```
## Performance note (DaitoMath)
`DaitoMath`
uses
`brick/math`
for precise decimal calculations (financial-safe).
-
It can run without extra PHP extensions.
-
For better performance on production, enable one of:
-
`ext-gmp`
(recommended)
-
`ext-bcmath`
-
`brick/math`
will automatically use the fastest available calculator at runtime.
Quick check:
```
bash
php -m | grep -Ei
"gmp|bcmath"
```
Install extensions if missing:
```
bash
# Ubuntu / Debian
sudo apt-get update
sudo apt-get install -y php-gmp php-bcmath
sudo systemctl restart php8.2-fpm
||
sudo systemctl restart apache2
```
```
bash
# CentOS / RHEL / Rocky / AlmaLinux
sudo yum install -y php-gmp php-bcmath
sudo systemctl restart php-fpm
||
sudo systemctl restart httpd
```
```
powershell
# Windows (php.ini)
# 1) Open php.ini
# 2) Enable these lines:
extension
=
gmp
extension
=
bcmath
# 3) Restart web server / PHP-FPM service
```
\ No newline at end of file
...
...
composer.json
View file @
cc727ce
...
...
@@ -8,7 +8,12 @@
}
},
"require"
:
{
"brick/math"
:
"^0.9 || ^0.10 || ^0.11 || ^0.13"
,
"php"
:
"^7.3 || ^8.0"
,
"laravel/framework"
:
"^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0"
},
"suggest"
:
{
"ext-gmp"
:
"Faster big-number calculations"
,
"ext-bcmath"
:
"Faster decimal calculations when GMP is unavailable"
}
}
...
...
composer.lock
View file @
cc727ce
...
...
@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies"
,
"This file is @generated automatically"
],
"content-hash"
:
"
f0e944fbfbea5c3b7cd02a9cd33e3cea
"
,
"content-hash"
:
"
0c23b9705a5fe293b678d1bbc5020207
"
,
"packages"
:
[
{
"name"
:
"brick/math"
,
...
...
src/DaitoMath.php
0 → 100644
View file @
cc727ce
<?php
namespace
Daito\Lib
;
use
Brick\Math\BigDecimal
;
use
Brick\Math\Exception\DivisionByZeroException
;
use
Brick\Math\RoundingMode
;
use
RuntimeException
;
class
DaitoMath
{
const
DEFAULT_SCALE
=
12
;
public
static
function
add
(
$left
,
$right
,
$scale
=
self
::
DEFAULT_SCALE
)
{
$bigDecimal
=
self
::
toBigDecimal
(
$left
)
->
plus
(
self
::
toBigDecimal
(
$right
));
return
self
::
normalize
((
string
)
$bigDecimal
->
toScale
((
int
)
$scale
));
}
public
static
function
sub
(
$left
,
$right
,
$scale
=
self
::
DEFAULT_SCALE
)
{
$bigDecimal
=
self
::
toBigDecimal
(
$left
)
->
minus
(
self
::
toBigDecimal
(
$right
));
return
self
::
normalize
((
string
)
$bigDecimal
->
toScale
((
int
)
$scale
));
}
public
static
function
mul
(
$left
,
$right
,
$scale
=
self
::
DEFAULT_SCALE
)
{
$bigDecimal
=
self
::
toBigDecimal
(
$left
)
->
multipliedBy
(
self
::
toBigDecimal
(
$right
));
return
self
::
normalize
((
string
)
$bigDecimal
->
toScale
((
int
)
$scale
));
}
public
static
function
div
(
$left
,
$right
,
$scale
=
self
::
DEFAULT_SCALE
)
{
$leftValue
=
self
::
toBigDecimal
(
$left
);
$rightValue
=
self
::
toBigDecimal
(
$right
);
if
(
$rightValue
->
isZero
())
{
throw
new
RuntimeException
(
'Can not divide by zero.'
);
}
try
{
$bigDecimal
=
$leftValue
->
dividedBy
(
$rightValue
,
(
int
)
$scale
,
RoundingMode
::
HALF_UP
);
}
catch
(
DivisionByZeroException
$exception
)
{
throw
new
RuntimeException
(
'Can not divide by zero.'
);
}
return
self
::
normalize
((
string
)
$bigDecimal
);
}
public
static
function
floor
(
$value
)
{
return
(
string
)
self
::
toBigDecimal
(
$value
)
->
toScale
(
0
,
RoundingMode
::
FLOOR
);
}
public
static
function
ceil
(
$value
)
{
return
(
string
)
self
::
toBigDecimal
(
$value
)
->
toScale
(
0
,
RoundingMode
::
CEILING
);
}
public
static
function
round
(
$value
,
$scale
=
0
)
{
return
self
::
normalize
((
string
)
self
::
toBigDecimal
(
$value
)
->
toScale
((
int
)
$scale
,
RoundingMode
::
HALF_UP
));
}
public
static
function
mulFloor
(
$left
,
$right
,
$scale
=
self
::
DEFAULT_SCALE
)
{
return
self
::
floor
(
self
::
mul
(
$left
,
$right
,
$scale
));
}
private
static
function
toBigDecimal
(
$value
)
{
if
(
is_int
(
$value
)
||
is_string
(
$value
))
{
return
BigDecimal
::
of
((
string
)
$value
);
}
if
(
is_float
(
$value
))
{
return
BigDecimal
::
of
(
self
::
normalize
(
sprintf
(
'%.14F'
,
$value
)));
}
throw
new
RuntimeException
(
'DaitoMath only supports int, float, or numeric string.'
);
}
private
static
function
normalize
(
$number
)
{
$number
=
(
string
)
$number
;
if
(
strpos
(
$number
,
'.'
)
===
false
)
{
return
$number
;
}
$number
=
rtrim
(
$number
,
'0'
);
$number
=
rtrim
(
$number
,
'.'
);
if
(
$number
===
'-0'
)
{
return
'0'
;
}
return
$number
===
''
?
'0'
:
$number
;
}
}
Please
register
or
sign in
to post a comment