Decimal::toFloat()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Postpay\Serializers;
4
5
use JsonSerializable;
6
7
class Decimal implements JsonSerializable
8
{
9
    /**
10
     * @const int Default number of decimals.
11
     */
12
    const DEFAULT_DECIMALS = 2;
13
14
    /**
15
     * @var int The stored value.
16
     */
17
    public $value;
18
19
    /**
20
     * Constructor.
21
     *
22
     * @param int $value
23
     */
24 2
    public function __construct($value)
25
    {
26 2
        $this->value = $value;
27 2
    }
28
29
    /**
30
     * Creates an instance using the specified format.
31
     *
32
     * @param DateTime $value
33
     * @param int      $decimals
34
     *
35
     * @return self
36
     */
37 2
    public static function fromFloat(
38
        $value,
39
        $decimals = self::DEFAULT_DECIMALS
40
    ) {
41 2
        return new self((int) round($value * 10 ** $decimals));
42
    }
43
44
    /**
45
     * Converts to float type.
46
     *
47
     * @param int $decimals
48
     *
49
     * @return float
50
     */
51 1
    public function toFloat($decimals = self::DEFAULT_DECIMALS)
52
    {
53 1
        return $this->value / 10 ** $decimals;
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59 1
    public function jsonSerialize()
60
    {
61 1
        return $this->value;
62
    }
63
}
64