2021-07-06 12:02:47 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
2022-06-21 11:57:17 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2021-07-06 12:02:47 +02:00
|
|
|
*/
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-07-06 12:02:47 +02:00
|
|
|
namespace Tests\Unit;
|
|
|
|
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
class CentConversionTest extends TestCase
|
|
|
|
{
|
2022-06-21 12:00:57 +02:00
|
|
|
protected function setUp() :void
|
2021-07-06 12:02:47 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConversionOfDecimalValues()
|
|
|
|
{
|
|
|
|
$precision = 2;
|
|
|
|
|
|
|
|
$amount = 10.20;
|
2022-06-21 11:57:17 +02:00
|
|
|
$amount = round(($amount * pow(10, $precision)), 0);
|
2021-07-06 12:02:47 +02:00
|
|
|
|
|
|
|
$this->assertEquals(1020, $amount);
|
|
|
|
|
|
|
|
$amount = 2;
|
2022-06-21 11:57:17 +02:00
|
|
|
$amount = round(($amount * pow(10, $precision)), 0);
|
2021-07-06 12:02:47 +02:00
|
|
|
|
|
|
|
$this->assertEquals(200, $amount);
|
|
|
|
|
|
|
|
$amount = 2.12;
|
2022-06-21 11:57:17 +02:00
|
|
|
$amount = round(($amount * pow(10, $precision)), 0);
|
2021-07-06 12:02:47 +02:00
|
|
|
|
|
|
|
$this->assertEquals(212, $amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBcMathWay()
|
|
|
|
{
|
|
|
|
$amount = 64.99;
|
|
|
|
$amount = bcmul($amount, 100);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-07-06 12:02:47 +02:00
|
|
|
$this->assertEquals(6499, $amount);
|
|
|
|
|
|
|
|
$amount = 2;
|
|
|
|
$amount = bcmul($amount, 100);
|
|
|
|
|
|
|
|
$this->assertEquals(200, $amount);
|
|
|
|
|
|
|
|
$amount = 2.12;
|
|
|
|
$amount = bcmul($amount, 100);
|
|
|
|
|
|
|
|
$this->assertEquals(212, $amount);
|
|
|
|
}
|
|
|
|
}
|