PHP Classes

SI Prefix Converter: Convert a number to a string appending a prefix

Recommend this page to a friend!
  Info   View files Example   Demos   Screenshots Screenshots   View files View files (14)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Last Updated Ratings Unique User Downloads Download Rankings
2023-06-02 (6 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 142 This week: 1All time: 9,132 This week: 319Up
Version License PHP version Categories
sci-convert 1.2GNU General Publi...5.3PHP 5, Conversion, Math
Description 

Author

This class can convert a number to a string appending a prefix from the International System of Units (SI).

It can take a number and calculate its order of magnitude to determine what is the prefix to be appended to the number, for instance converting 250000 to 250K.

The conversion can be configured to define rounding rules, the numeric base, and the text of the prefixes.

In Spanish:

Esta clase permite hacer conversiones entre un numero (esté en un formato numerico o en un string) y su representación con prefijos en cualquier base. (ej, se puede convertir entre 250000 y 250K -prefijos SI- o 244.14Ki -prefijos binarios-)

Se puede crear un sistema personalizado (base y prefijos con sus valores) real o ficticio; guardando sus configuraciones en archivos distintos.

Realiza conversiones bidireccionales (de numero a formato con prefijos y de formato con prefijos a numero, siempre y cuando la codificación esté configurada correctamente), y se puede ajustar el modo de redondeo (decimales o cifras significativas) para la conversion hacia prefijos. Tambien se puede escoger si se quiere el numero resultante, unido al prefijo; o por aparte para darle un tratamiento independiente a ambas partes.

Innovation Award
PHP Programming Innovation award nominee
June 2014
Number 6


Prize: One downloadable copy of Komodo IDE
Sometimes it is necessary to display numbers in an abbreviated format.

This class provides a solution that can abbreviate numbers by appending prefixes like KB, MB, GB, TB, etc..

Manuel Lemos
Picture of Alex F. Bustos
  Performance   Level  
Name: Alex F. Bustos <contact>
Classes: 1 package by
Country: Colombia Colombia
Age: 28
All time rank: 427029 in Colombia Colombia
Week rank: 247 Up1 in Colombia Colombia Up
Innovation award
Innovation award
Nominee: 1x

Details
File: readme.txt Created by: Alex Bustos - @alexfbp At first, Thanks for downloading and testing it. This file is a little resume and explanation on how to use this class. For specific details, see the class and demo files. ======================================= INITIALIZATION STEPS 1. Include, instantiate and (opt) set the numeric and list limitations include('settings.inc.php'); //Maybe you want to change some default values include('sci.class.php'); $sci=new sci('LOP'[,- more options-]); //See the __construct() function for details. If the first argument of the construct is a variable, you must be sure that this variable is sanitized, has not forbidden expressions such as "..", "/", etc... The 'LOP' (List Of Prefixes) are the DESIRED list, not the used list which are generated internally through a call to the start() function (step 4) 2. (opt) Set the numeric out format. By decimal places or significant digits $sci->set_round_params(); 3. (opt) Insert Additive Rules $sci->set_single_rule(); $sci->set_scale_rule(); $sci->set_all_rule(); 4. $sci->start(); //the object. Else, the main functions will return the unmodified value ======================================= MAIN FUNCTIONS: mixed get_prefixed(mixed $value,bool $array=false): Returns a string like $number.$join.$prefix. based on a previously choosen List of Prefixes. The $join value was previously defined when instantiating the object. Or, if you want to format the prefix as you want, you can set $array=true, and the function will return an array($number,$prefix); mixed get_number(string $string, bool $entities=true, $flags=ENT_COMPAT) If you want to do the inverse process, here comes that function. May return a float or a int. Prior to use this function, you should configure the charset. ======================================= CONFIGURATION FUNCTIONS: sci::valid_list($LOP) - Basic validation if a List of Prefixes $LOP are in the allowed format. set_round_params() - Rounding Configuration. See the class and then http://www.php.net/manual/en/function.round.php set_charset($charset) - Valid Charset. See the $encoding arg of https://php.net/manual/es/function.htmlentities.php ======================================= RULE FUNCTIONS: Add to the ALLOWED list, from the DESIRED list (and if the exponent are valid in the specified range): set_single_rule($exponent); //a prefix of the same exponent set_scale_rule($scale); //each prefix multiple of $scale Omitting the limits, ej: with 3, allowed exps will have too {...,-9,-6,-3,0,3,6,9,...} with 2, allowed exps will have too {...,-6,-4,-2,0,2,4,6,...} and so... set_all_rule(); //All of the prefixes If needed, to understand the differences between the allowed and desired lists, var_dump an sci object, before and after to the call to start(): $sci=new sci(); var_dump($sci); ... //Maybe you wish to add some rules $sci->start(); var_dump($sci);
  Example includedExternal page  

Open in a separate window

Screenshots  
  • screenshot_example.png
  Files folder image Files  
File Role Description
Files folder imagesets (10 files)
Plain text file sci.class.php Class Main class
Accessible without login Plain text file examples.php Example Many possible uses of the class
Accessible without login Plain text file README.txt Doc. Brief of how to use the class
Accessible without login Plain text file settings.inc.php Conf. Default settings of the class

  Files folder image Files  /  sets  
File Role Description
  Accessible without login Plain text file binary_long.set.php Aux. Binary prefixes in long format
  Accessible without login Plain text file binary_short.set.php Aux. Binary prefixes in short format
  Accessible without login Plain text file README.txt Doc. Considerations about the Lists of prefixes
  Accessible without login Plain text file sets-README.txt Doc. Documentation
  Accessible without login Plain text file si_long.set.php Aux. SI prefixes in long format
  Accessible without login Plain text file si_long_es.set.php Aux. Copy of "long", but changing the 10e-3 prefix to mili
  Accessible without login Plain text file si_short.set.php Aux. SI prefixes in short format
  Accessible without login Plain text file si_short_html.set.php Aux. SI prefixes in short format and HTML entities for micro
  Accessible without login Plain text file si_short_strict.set.php Aux. Introduced in 1.2, according wit the CGPM
  Accessible without login Plain text file si_short_strict_html.set.php Aux. Introduced in 1.2, according wit the CGPM

 Version Control Unique User Downloads Download Rankings  
 93%
Total:142
This week:1
All time:9,132
This week:319Up