Litipk\BigNumbers\Decimal::fromDecimal PHP Méthode

fromDecimal() public static méthode

Constructs a new Decimal object based on a previous one, but changing it's $scale property.
public static fromDecimal ( Decimal $decValue, integer $scale = null ) : Decimal
$decValue Decimal
$scale integer
Résultat Decimal
    public static function fromDecimal(Decimal $decValue, $scale = null)
    {
        self::paramsValidation($decValue, $scale);
        // This block protect us from unnecessary additional instances
        if ($scale === null || $scale >= $decValue->scale || $decValue->isInfinite()) {
            return $decValue;
        }
        return new static(self::innerRound($decValue->value, $scale), $scale);
    }

Usage Example

 public function testBasicCase()
 {
     $n1 = Decimal::fromString('3.45');
     $this->assertTrue(Decimal::fromDecimal($n1)->equals($n1));
     $this->assertTrue(Decimal::fromDecimal($n1, 2)->equals($n1));
     $this->assertTrue(Decimal::fromDecimal($n1, 1)->equals(Decimal::fromString('3.5')));
     $this->assertTrue(Decimal::fromDecimal($n1, 0)->equals(Decimal::fromString('3')));
 }
All Usage Examples Of Litipk\BigNumbers\Decimal::fromDecimal