PHP code example of ferdiunal / money

1. Go to this page and download the library: Download ferdiunal/money library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ferdiunal / money example snippets

 php


use Ferdiunal\Money\Money;

// Create a new Money instance with a value of 100.50
$money = Money::make(100.50);

// Add 50.25 to the Money instance
$money->sum(50.25);

// Subtract 10.50 from the Money instance
$money->subtract(10.50);

// Multiply the Money instance by 2
$money->multiply(2);

// Divide the Money instance by 3
$money->divide(3);

// Add a 20% tax to the Money instance
$money->addTax(20);

// Add a fixed discount of 15 to the Money instance
$money->addDiscount(15, true);

// Add a 10% discount to the Money instance
$money->addDiscount(10);

// Remove the 20% tax from the Money instance
$money->removeTax(20);

// Enable locale usage and set the locale code to USD
$money->setLocaleActive(true)->setLocaleCode('USD');

// Get the Money instance as a formatted string
$formattedMoney = $money->get();

// Get the tax amount as a formatted string
$taxAmount = $money->getTax();

// Get the discount amount as a formatted string
$discountAmount = $money->getDiscount();

// Get the Money instance and tax amount as an array
$allData = $money->all();

// Output the formatted string
echo $formattedMoney; // $208.00

// Output the tax amount
echo $taxAmount; // $31.20

// Output the discount amount
echo $discountAmount; // $28.20

// Output the Money instance and tax amount as an array
print_r($allData); // Array ( [money] => 179.80 [tax] => 31.20, [discount] => 28.20 )