Bug #74655 [NEW]: parse int and float distort data
From: an dot nguyen-hoai at lazada dot com
Operating system: Docker Ubuntu
PHP version: 7.0.19
Package: Math related
Bug Type: Bug
Bug description:parse int and float distort data
Description:
------------
<?php
declare(strict_types=1);
namespace App\Traits;
/**
* Class SanitizerHelper
*
* @package App\Traits
*/
trait SanitizerHelperTrait
{
/**
* @param array $data
* @param array $fields
*
* @return array
*/
public function decimalFormat(array $data, array $fields)
{
foreach ($fields as $field) {
$value = array_get($data, $field, null);
if ($value) {
$value = ((float) $value) * 100;
$data[$field] = (int) $value;
}
}
return $data;
}
}
Test script:
---------------
<?php
namespace Tests\Unit\Traits;
use App\SanitizerHelperTrait;
/**
* Class SanitizerHelperTest
*
* @package Tests\Unit\Traits
*/
class SanitizerHelperTest extends \PHPUnit_Framework_TestCase
{
use SanitizerHelperTrait;
/**
* @param $data
* @param $expected
*
* @covers \App\Traits\SanitizerHelperTrait::decimalFormat()
* @dataProvider getDecimalDataProvider
*/
public function test_Decimal_Format($data, $expected)
{
static::assertEquals($expected, $this->decimalFormat($data,
array_keys($data)));
}
public function getDecimalDataProvider()
{
return [
'#1 decimal' => [
'data' => [
'discountAmount' => 17.99,
],
'expected' => [
'discountAmount' => 1799,
],
],
'#2 decimal' => [
'data' => [
'arAmount' => 10.02,
],
'expected' => [
'arAmount' => 1002,
],
],
'#3 decimal' => [
'data' => [
'gotAmount' => 10.02,
],
'expected' => [
'gotAmount' => 1002,
],
],
'#5 No decimal' => [
'data' => [
'arAmount' => 200,
],
'expected' => [
'arAmount' => 20000,
],
],
'#6 Null' => [
'data' => [
'arAmount' => null,
],
'expected' => [
'arAmount' => 0,
],
],
];
}
}
Expected result:
----------------
1799
Actual result:
--------------
1798
--
Edit bug report at https://bugs.php.net/bug.php?id=74655&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=74655&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=74655&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=74655&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=74655&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=74655&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=74655&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=74655&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=74655&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=74655&r=support
Expected behavior: https://bugs.php.net/fix.php?id=74655&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=74655&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=74655&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=74655&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=74655&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=74655&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=74655&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=74655&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=74655&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=74655&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=74655&r=mysqlcfg
Thread (2 messages)
- an dot nguyen-hoai at lazada dot com