From a8902788445c63f5b4ae6fdc8227e7fbbf0e3b9f Mon Sep 17 00:00:00 2001 From: Denis Girko Date: Sun, 13 Dec 2020 10:19:33 +0300 Subject: [PATCH] Added toFloat method --- src/RtLopez/Decimal.php | 7 ++++++- tests/FormatTest.php | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/RtLopez/Decimal.php b/src/RtLopez/Decimal.php index 800ae03..e88efbc 100644 --- a/src/RtLopez/Decimal.php +++ b/src/RtLopez/Decimal.php @@ -122,7 +122,12 @@ public function __toString() { return $this->toString(); } - + + public function toFloat() + { + return floatval($this->toString()); + } + public function format($prec = null, $dec_point = null , $thousands_sep = null, $trailing_zero = true) { if ($dec_point === null) { diff --git a/tests/FormatTest.php b/tests/FormatTest.php index bf916f4..055028e 100644 --- a/tests/FormatTest.php +++ b/tests/FormatTest.php @@ -163,4 +163,14 @@ public function testFormatNoNegativeZero($class) $res = new $class('-0.0001', 4); $this->assertSame('0.00', $res->format(2)); } + + /** + * @dataProvider providerClasses + */ + public function testToFloat($class) + { + $res = new $class('1.2345', 4); + $this->assertInternalType('float', $res->toFloat()); + $this->assertEquals(1.2345, $res->toFloat()); + } }