PHP code example of cable8mm / n-format

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

    

cable8mm / n-format example snippets


// config/n-format.php
return [
    'locale' => 'ko_KR',
    'currency' => 'KRW',
];

use Cable8mm\NFormat\NFormat;

NFormat::currency(358762);        // ₩358,762
NFormat::decimal(12346);          // 12,346
NFormat::spellOut(5);             // 오
NFormat::ordinalSpellOut(10);     // 열번째
NFormat::currencySpellOut(12346); // 12,346 원
NFormat::percent(12346);          // 1,234,600%
NFormat::rawPercent(12346);       // 12,346%

NFormat::currency(
    number: 12346,
    zero: '-',
    locale: 'ja_JP',
    currency: 'JPY',
); // ¥12,346

NFormat::price(number: 12346, roundDigits: -2); // 12300

NFormat::$locale = 'ja_JP';
NFormat::spellOut(5); // 五

NFormat::currency(12346, '0', 'ja_JP', 'JPY'); // ¥12,346

NFormat::price(12346, -2); // 12300
NFormat::smartPrice(12346); // 12300
NFormat::smartPrice(1234678); // 1230000

use Cable8mm\NFormat\Casts\AsCurrency;
use Cable8mm\NFormat\Casts\AsNumber;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected function casts(): array
    {
        return [
            'price' => AsCurrency::class,
            'jpy' => AsCurrency::class.':ja_JP,JPY',
            'discount' => AsNumber::class,
            'count' => AsNumber::class.':ja_JP',
        ];
    }
}

$product = new Product;
$product->price = '₩12,350원';

echo $product->price;                // 12350 (raw value)
echo $product->price->currency();    // ₩12,350
echo $product->price->price(-2);     // 12400
echo $product->price->smartPrice();  // 12400
echo $product->price->spellOut();    // 12,350 원
echo $product->price->value();       // 12350

$product->price = 0;
echo $product->price;                // 0 (raw value)
echo $product->price->currency();    // ₩0
echo $product->price->display();     // 무료
echo $product->price->freeLabel();   // 무료
echo $product->price->spellOut();    // 0 원
echo $product->price->displaySpelled(); // 무료

$product->discount = 12346;

echo $product->discount;                    // 12,346
echo $product->discount->percent();         // 1,234,600%
echo $product->discount->rawPercent();      // 12,346%
echo $product->discount->spellOut();        // 일만이천삼백사십육
echo $product->discount->ordinalSpellOut(); // 열번째
echo $product->discount->value();            // 12346

use Cable8mm\NFormat\Drivers\Contracts\OrdinalDriver;

final class EnUsOrdinalDriver implements OrdinalDriver
{
    public function spellOut(int $number): string
    {
        return match ($number) {
            1 => 'first',
            2 => 'second',
            default => $number.'th',
        };
    }
}

NFormat::registerOrdinal('en_US', new EnUsOrdinalDriver);
NFormat::ordinalSpellOut(2, 'en_US'); // second

use Cable8mm\NFormat\Drivers\Contracts\CurrencyDriver;

final class UsdCurrencyDriver implements CurrencyDriver
{
    public function currencySpellOut(string $formatted): string
    {
        return $formatted.' dollars';
    }

    public function roundDigits(): array
    {
        return [3 => -1, 4 => -2, 5 => -2];
    }
}

NFormat::registerCurrency('USD', new UsdCurrencyDriver);