PHP code example of jacobfitzp / weight-conversions

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

    

jacobfitzp / weight-conversions example snippets


echo (new Pound(27.8))
    ->to(Kilogram::class)
    ->round()
    ->formatted()

// 12.6kg

// Create a new measurement unit
$kilograms = new Kilogram();

// Set weight amount
// Can also be passed to the constructor
$kilograms->setAmount(5.75);

// Convert to a different unit type
$ounce = $kilograms->to(Ounce::class);

// Get weight amount
$ounce->amount();

// Round the amount
// Defaults to a precision of 1 decimal place
$ounce->round();

// Get a formatted human-readable version of the amount
// For example "2kgG", "12st 5" 
$ounce->formatted();

class Hobnobs extends AbstractUnit
{
    public const RELATIVE_TO_KG = 50;
    
    public function formatted(string $format = '%s hobnobs'): string
    {
        return sprintf($format, $this->amount());
    }
}