Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit 59e8417

Browse files
committed
Merge pull request #6 from c4pone/accessors
Merge from @c4pone: Added asFloat asInteger and _innerValue methods
2 parents 953017b + 1518a3c commit 59e8417

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

src/Decimal.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,37 @@ private static function countSignificativeDigits(Decimal $val, Decimal $abs)
769769
) - ($val->isNegative() ? 1 : 0);
770770
}
771771

772+
/**
773+
* Return value as a float
774+
*
775+
* @return float
776+
*/
777+
public function asFloat()
778+
{
779+
return floatval($this->value);
780+
}
781+
782+
/**
783+
* Return value as a integer
784+
*
785+
* @return float
786+
*/
787+
public function asInteger()
788+
{
789+
return intval($this->value);
790+
}
791+
792+
/**
793+
* Return the inner representation of the class
794+
* use with caution
795+
*
796+
* @return number
797+
*/
798+
public function _innerValue()
799+
{
800+
return $this->value;
801+
}
802+
772803
/**
773804
* @return string
774805
*/

tests/Decimal/DecimalAsFloatTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use Litipk\BigNumbers\Decimal as Decimal;
4+
5+
6+
date_default_timezone_set('UTC');
7+
8+
9+
class DecimalAsFloatTest extends PHPUnit_Framework_TestCase
10+
{
11+
public function testFloat()
12+
{
13+
$this->assertEquals(1.0, Decimal::fromString('1.0')->asFloat());
14+
$this->assertTrue(is_float(Decimal::fromString('1.0')->asFloat()));
15+
16+
$this->assertEquals(1.0, Decimal::fromInteger(1)->asFloat());
17+
$this->assertTrue(is_float(Decimal::fromInteger(1)->asFloat()));
18+
19+
$this->assertEquals(1.0, Decimal::fromFloat(1.0)->asFloat());
20+
$this->assertEquals(1.123123123, Decimal::fromString('1.123123123')->asFloat());
21+
22+
$this->assertTrue(is_float(Decimal::fromFloat(1.0)->asFloat()));
23+
$this->assertTrue(is_float(Decimal::fromString('1.123123123')->asFloat()));
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use Litipk\BigNumbers\Decimal as Decimal;
4+
5+
6+
date_default_timezone_set('UTC');
7+
8+
9+
class DecimalAsIntegerTest extends PHPUnit_Framework_TestCase
10+
{
11+
public function testFloat()
12+
{
13+
$this->assertEquals(1, Decimal::fromString('1.0')->asInteger());
14+
$this->assertTrue(is_int(Decimal::fromString('1.0')->asInteger()));
15+
16+
$this->assertEquals(1, Decimal::fromInteger(1)->asInteger());
17+
$this->assertTrue(is_int(Decimal::fromInteger(1)->asInteger()));
18+
19+
$this->assertEquals(1, Decimal::fromFloat(1.0)->asInteger());
20+
$this->assertEquals(1, Decimal::fromString('1.123123123')->asInteger());
21+
22+
$this->assertTrue(is_int(Decimal::fromFloat(1.0)->asInteger()));
23+
$this->assertTrue(is_int(Decimal::fromString('1.123123123')->asInteger()));
24+
}
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use Litipk\BigNumbers\Decimal as Decimal;
4+
5+
6+
date_default_timezone_set('UTC');
7+
8+
9+
class DecimalInnerValueTest extends PHPUnit_Framework_TestCase
10+
{
11+
public function testInnerValue()
12+
{
13+
for($i=0;$i<100;$i++)
14+
{
15+
$this->assertEquals($i, Decimal::fromInteger($i)->_innerValue());
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)