PHP code example of penangites / number

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

    

penangites / number example snippets


use Penangites\Number\Number;
use Penangites\Number\Percentage;

Number::of('19.90')
    ->tax(Percentage::fromPercent('6'))
    ->toString(); // "21.094" — exact, no rounding until you ask

Number::of('19.90')
    ->tax(Percentage::fromPercent('6'))
    ->round(2)
    ->toString(); // "21.09"

Number::of('19.90');  // strings are exact — prefer them
Number::of(100);      // integers are exact
Number::of(1.5);      // floats accepted, stored as the double really holds them
Number::of('1.0E-5'); // scientific notation is expanded exactly

Number::of(0.1 + 0.2)->toString();  // "0.30000000000000004" — never was 0.3
Number::of('0.1')->add('0.2');      // "0.3" — strings stay exact

$n = Number::of('19.90');

$n->toString();  // "19.9" — canonical decimal string
(string) $n;     // same
$n->toFloat();   // 19.9
$n->toInt();     // throws — has a fractional part; round() first
json_encode($n); // '"19.9"' — serialises as the exact string

Number::of('0.1')->add('0.2')->toString();      // "0.3" — no float drift
Number::of('0.3')->subtract('0.1')->toString(); // "0.2"
Number::of('1.5')->multiply('2.5')->toString(); // "3.75" — always exact
Number::of('1.5')->negate()->toString();        // "-1.5"
Number::of('-1.5')->abs()->toString();          // "1.5"

use Penangites\Number\RoundingMode;

Number::of(1)->divide(3)->toString();                        // "0.333333333333"
Number::of(10)->divide(3, 2)->toString();                    // "3.33"
Number::of(10)->divide(3, 2, RoundingMode::Up)->toString();  // "3.34"

Number::of('2.5')->round()->toString();     // "3"  — half away from zero
Number::of('1.005')->round(2)->toString();  // "1.01" — floats get this wrong

Number::of('1.204')->round(2)->toString(); // "1.2", not "1.20"

$rate = Percentage::fromPercent('6');   // 6%
$rate = Percentage::fromRatio('0.06');  // same thing (1 = 100%)

$rate->toPercent(); // "6"
$rate->toRatio();   // "0.06"
$rate->toFloat();   // 0.06 — approximate; use toRatio() when exact
$rate->toArray();   // ['ratio' => '0.06', 'percent' => '6']
json_encode($rate); // '"0.06"' — reads back with fromRatio()

$rate = Percentage::fromRatio('0.06'); // 6%

$rate->add(Percentage::fromPercent('2'))->toPercent(); // "8"
$rate->greaterThan(Percentage::fromRatio('0.05'));     // true
$rate->isPositive();                                   // true

$price = Number::of('19.90');
$tax = Percentage::fromPercent('6');

$price->percentOf($tax)->toString();  // "1.194" — the tax amount
$price->tax($tax)->toString();        // "21.094" — price + tax
$price->discount($tax)->toString();   // "18.706" — price - discount

$total = Number::of('199.99')
    ->discount(Percentage::fromPercent('15'))
    ->tax(Percentage::fromPercent('8'))
    ->round(2);

$total->toString(); // "183.59"

$a = Number::of('1.5');

$a->equals('1.50');       // true — compared by value
$a->greaterThan(1);       // true
$a->lessThanOrEqual($a);  // true
$a->compareTo(2);         // -1
$a->isZero();
$a->isPositive();
$a->isNegative();

$number = Number::of(100);

$english = new NumberFormatter('en', NumberFormatter::SPELLOUT);
$chinese = new NumberFormatter('zh', NumberFormatter::SPELLOUT);

$english->format($number->toFloat()); // "one hundred"
$chinese->format($number->toFloat()); // "一百"