Search
 
SCRIPT & CODE EXAMPLE
 

PHP

how to convert number into million and billion suffix in PHP using brick/Money package

    private const Million = 'million';
    private const Billion = 'billion';

    public function format(Money $money)
    {
        $million = Money::of(1000000, 'GBP');
        $billion = Money::of(1000000000, 'GBP');

        $formatter = new NumberFormatter('en_GB', NumberFormatter::CURRENCY);

        if ($money->isGreaterThanOrEqualTo($billion)) {
            return $this->formatWithSuffix($money, $billion, self::Billion, $formatter);
        }

        if ($money->isGreaterThanOrEqualTo($million)) {
            return $this->formatWithSuffix($money, $million, self::Million, $formatter);
        }

        $formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, 0);

        return $money->formatWith($formatter);
    }

    protected function formatWithSuffix(Money $money, Money $scale, string $suffix, $formatter)
    {
        $formatter->setPattern("£#0.## $suffix");

        return $money->dividedBy($scale->getAmount(), RoundingMode::UP)->formatWith($formatter);
    }
Comment

PREVIOUS NEXT
Code Example
Php :: laravel return response with headers 
Php :: laravel relationship hasmany 
Php :: auto complete order paid1 
Php :: Total Number Of Words 
Php :: This behaviour is (currently) not supported by Doctrine 2 
Php :: Number in English Words (Indian format) php 
Php :: php like button counter 
Php :: how to change laravel logo image 
Php :: run specific seeder laravel 
Php :: laravel api get controller 
Php :: php calculate variance 
Php :: laravel eloquent query with orderBy subquery 
Php :: HTTP Authentication example forcing a new name/password 
Php :: laravel eloquent get current sequence value 
Php :: Multiple databases user validation in Laravel 
Php :: tina4 create route 
Php :: livewire layout error 
Php :: gmail smtp not working laravel 
Php :: php zoom api start_time issue 
Php :: Drupal config_readonly 
Php :: WooCommerce Catalog Mode snippets 
Php :: laravel migration softdelete 
Php :: php read textarea line by line 
Php :: php artisan tinker new record 
Php :: fichier wp-config.php 
Php :: wp automatic-feed-links 
Php :: PHP DocBlocker current date 
Php :: Eagar loading,parent child relationship,Sub relationship in laravel 
Php :: acf directions map link 
Php :: laravel length validation 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =