PHP code example of kodeas / currency

1. Go to this page and download the library: Download kodeas/currency 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/ */

    

kodeas / currency example snippets


MyModel extends Model
{
    protected $casts = [
        'amount' => Kodeas\Currency\Casts\Currency::class
    ];
}

$currency = Kodeas\Currency\Currency::fromUsd(1);

$model = MyModel::create([
    'amount' => $currency //100(cents) in database
]);

$model->amount //Currency::class

$currency = Kodeas\Currency\Currency::fromUsd(1);
$currency = Kodeas\Currency\Currency::fromCents(100);

echo $currency; // "1.00"
$currency->toUsd(); // "1"
$currency->toCents(); // "100"
$currency->toReadable(); // "1.00"
$currency->toReadable('$'); // "$1.00"
return response()->json(['currency' => $currency->toUsd()]); // {"currency": "1.00"}