PHP code example of code-distortion / realnum

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

    

code-distortion / realnum example snippets

 php
RealNum::new(0.12345678901234567890);   // "0.12345678901235" (precision lost because the number passed is a PHP float)
RealNum::new('0.12345678901234567890'); // "0.12345678901234567890" (passed as a string)
 php
$num = RealNum::new();
print $num->locale;             // "en"
print $num->maxDecPl;           // 20 (the maximum number of decimal places used)
print $num->immutable;          // true
var_dump($num->formatSettings); // ['null' => null, 'trailZeros' => null … ]
 php
RealNum::new(5)->isNull();
 php
$num = RealNum::new(1234567.89);
print $num->format(); // "1,234,567.89"
 php
print RealNum::new(1234567.89)->format('!thousands showPlus locale=de-DE'); // "+1234567,89"
 php
print (string) RealNum::new(1234567.89); // "1,234,567.89"
 php
$num1 = RealNum::new(1234567.89)->formatSettings('!thousands showPlus');
print $num1->format(); // "+1234567.89" (no thousands separator, show-plus)
 php
var_dump(RealNum::getDefaultFormatSettings()); // ['null' => null, 'trailZeros' => false … ] (default)
RealNum::setDefaultFormatSettings('null="NULL" trailZeros');
var_dump(RealNum::getDefaultFormatSettings()); // ['null' => 'NULL', 'trailZeros' => true … ]
 php
print RealNum::new(1234567.89)->format('locale=fr-FR'); // "1 234 567,89"
 php
RealNum::setDefaultLocale('fr-FR');
print RealNum::getDefaultLocale(); // "fr-FR"
 php
$num = RealNum::new('0.123456789012345678901234567890'); // passed as a string to maintain precision
print $num->val; // "0.12345678901234567890" ie. rounded to the default 20 decimal places
$num = RealNum::new()->maxDecPl(30)->val('0.123456789012345678901234567890');
print $num->val; // "0.123456789012345678901234567890" the full 30 decimal places
 php
RealNum::setDefaultMaxDecPl(30);
print RealNum::getDefaultMaxDecPl(); // 30
 php
$num1 = RealNum::new(1)->immutable(false);
$num2 = $num1->add(2); // $num1 is changed and $num2 points to the same object
print $num1->format(); // "3"
print $num2->format(); // "3"
 php
RealNum::setDefaultImmutability(false);
var_dump(RealNum::getDefaultImmutability()); // "bool(false)"
 php
print RealNum::new(1)
->locale('en-US')->val(5)->maxDecPl(3) // some "setting" methods
->add(4)->mul(3)->div(2)->sub(1)       // some "calculation" methods
->format(); // "12.5"
 php
'providers' => [
    …
    CodeDistortion\RealNum\Laravel\ServiceProvider::class,
    …
],
 bash
php artisan vendor:publish --provider="CodeDistortion\RealNum\Laravel\ServiceProvider" --tag="config"