Skip to content
This repository has been archived by the owner on Jul 8, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature-bc-math'
Browse files Browse the repository at this point in the history
  • Loading branch information
corpsee committed Sep 1, 2018
2 parents fcea38a + 6bbd136 commit 368eb39
Show file tree
Hide file tree
Showing 15 changed files with 693 additions and 111 deletions.
50 changes: 25 additions & 25 deletions .php-censor.yml
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
build_settings:
clone_depth: 1
ignore:
clone_depth: 1
ignore:
- vendor
- tests

setup:
composer:
action: install
composer:
action: install

test:
php_unit:
config:
- phpunit.xml
php_unit:
config:
- phpunit.xml

php_mess_detector:
allow_failures: true
php_mess_detector:
allow_failures: true

php_code_sniffer:
standard: PSR2
encoding: UTF-8
allow_failures: true
php_code_sniffer:
standard: PSR2
encoding: UTF-8
allow_failures: true

php_cpd:
allow_failures: true
php_cpd:
allow_failures: true

php_loc:
allow_failures: true
php_loc:
allow_failures: true

php_parallel_lint:
allow_failures: true
php_parallel_lint:
allow_failures: true

php_docblock_checker:
allow_failures: true
php_docblock_checker:
allow_failures: true

security_checker:
allow_failures: true
security_checker:
allow_failures: true

complete:
email:
default_mailto_address: [email protected]
email:
default_mailto_address: [email protected]
30 changes: 15 additions & 15 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@ language: php
sudo: false

cache:
directories:
directories:
- $HOME/.composer/cache

php:
- 5.6
- 7.0
- 7.1
- 7.2
- 5.6
- 7.0
- 7.1
- 7.2

matrix:
fast_finish: true
fast_finish: true

install:
- composer selfupdate
- composer install
- composer selfupdate
- composer install

script:
- vendor/bin/phpunit --coverage-clover=coverage.xml
- vendor/bin/phpunit --coverage-clover=coverage.xml

after_success:
- bash <(curl -s https://codecov.io/bash)
- bash <(curl -s https://codecov.io/bash)

notifications:
email:
recipients:
- [email protected]
email:
recipients:
- [email protected]

on_success: always
on_failure: always
on_success: always
on_failure: always
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,47 @@ use Nameless\Utilities\UrlHelper;
echo UrlHelper::toPath('/path/to/url', '/base'); // Prints '/base/path/to/url'
```

### BcMathHelper

Passing values of type float to a BCMath function which expects a string as operand may not have the desired effect
due to the way PHP converts float values to string, namely that the string may be in exponential notation (what is not
supported by BCMath), and that the decimal separator is locale depended (while BCMath always expects a decimal point).

```php
<?php

$num1 = 0; // (string) 0 => '0'
$num2 = -0.000005; // (string) -0.000005 => '-5.05E-6'

echo bcadd($num1, $num2, 6); // => '0.000000'

setlocale(LC_NUMERIC, 'de_DE'); // uses a decimal comma
$num2 = 1.2; // (string) 1.2 => '1,2'

echo bcsub($num1, $num2, 1); // => '0.0'

?>
```

BcMathHelper solve problem with floats to strings converting and "," as decimal separator for BcMath functions:

```php
use Nameless\Utilities\BcMathHelper;

var_dump(BcMathHelper::add('0.000005', '0.000005', 5)); // (float)0.00001
var_dump(BcMathHelper::add('0,000005', '0,000005', 5)); // (float)0.00001
var_dump(BcMathHelper::add(0.000005, 0.000005, 5)); // (float)0.00001
var_dump(BcMathHelper::add('5.0E-6', '5.0E-7', 5)); // (float)0.00001

var_dump(BcMathHelper::sub(0.000005, 0.000001, 5)); // (float)0.000004

var_dump(BcMathHelper::mul(0.000005, 0.000002, 11)); // (float)0.00000000001

var_dump(BcMathHelper::div(0.000005, 0.000002, 2)); // (float)2.50

var_dump(BcMathHelper::comp(0.000005, 0.000002, 6)); // (int)1
```

Tests
-----

Expand Down
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,20 @@
},
"require": {
"php": ">=5.6.0",
"ext-mbstring": "*"
"ext-mbstring": "*",
"ext-bcmath": "*"
},
"require-dev": {
"phpunit/phpunit": "~5.7",
"symfony/yaml": "~3.4",
"doctrine/instantiator": "1.0.*",
"phpunit/php-token-stream": "~1.0",
"phpdocumentor/reflection-docblock": "~2.0"
"phpdocumentor/reflection-docblock": "~2.0",
"myclabs/deep-copy": "~1.7.0"
},
"extra": {
"platform": {
"php": "5.6.*"
}
}
}
106 changes: 83 additions & 23 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 368eb39

Please sign in to comment.