PHP Classes

PHP Order of Magnitude Prefix: Get the name of the prefix for a measure value

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 39 All time: 10,911 This week: 555Up
Version License PHP version Categories
magnitudeprefix 1.0GNU Lesser Genera...5PHP 5, Conversion
Description 

Author

This class can get the name of the prefix for a measure value.

It takes as parameter the number of a measured value like for instance: seconds, meters, grams, etc..

The class returns string that can be used to represent the same number of the measure value using a prefix like Kilo, Mega, Giga, Tera, mili, micro, nano, pico, etc.. For example, "7000 meters" can be displayed as "7 Kilo meters".

Picture of Alejandro Bazan
Name: Alejandro Bazan <contact>
Classes: 2 packages by
Country: Argentina Argentina
Age: 47
All time rank: 397156 in Argentina Argentina
Week rank: 195 Up4 in Argentina Argentina Up

Example

<?php
require_once "magnitudePrefix.class.php";

$mp = new MagnitudePrefix();

//Some numbers
echo"<br>Some Numbers<br>";
$number = 0.2;
echo
$number . " => " . $mp->getPrefixed($number, 'seconds') . "<BR/>";

$number = 0.002;
echo
$number . " => " . $mp->getPrefixed($number, 'grams') . "<BR/>";

$number = 200;
echo
$number . " => " . $mp->getPrefixed($number, 'Volts') . "<BR/>";

$number = 200000;
echo
$number . " => " . $mp->getPrefixed($number, 'Ohms') . "<BR/>";

$number = 0.00002;
echo
$number . " => " . $mp->getPrefixed($number, 'Farads') . "<BR/>";

//An expression, result from random calculation
echo"<br>An expression, result from random calculation<br>";
$number = round(rand() * exp(rand(-23,23)),2);
echo
$number . " => " . $mp->getPrefixed($number, 'meters') . "<BR/>";

//Expand scale
echo"<br>Expand scale<br>";
$mp->setScaleToExpandedStandard();
$number = 0.25;
echo
$number . " => " . $mp->getPrefixed($number, 'meters') . "<BR/>";

//Use Years
echo"<br>Use Years<br>";
$mp->setScaleToYears();
$number = 250;
echo
$number . " => " . $mp->getPrefixed($number, '') . "<BR/>";

//Custom scale
echo"<br>Custom scale<br>";
$mp->setScaleToCustom(array(
            array(
'factor'=>1E-3, 'prefix'=>'mili'),
            array(
'factor'=>1E-2, 'prefix'=>'centi'),
            array(
'factor'=>1, 'prefix'=>''),
            array(
'factor'=>1E3, 'prefix'=>'Kilo')
        )
);
$number = 250;
echo
$number . " => " . $mp->getPrefixed($number, 'meters') . "<BR/>";
$number = 0.43;
echo
$number . " => " . $mp->getPrefixed($number, 'meters') . "<BR/>";
$number = 0.005;
echo
$number . " => " . $mp->getPrefixed($number, 'meters') . "<BR/>";

?>


  Files folder image Files (2)  
File Role Description
Accessible without login Plain text file examples.php Example Examples
Plain text file magnitudePrefix.class.php Class The magnitudePrefix class

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:39
This week:0
All time:10,911
This week:555Up