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    
Last Updated Ratings Unique User Downloads Download Rankings
2016-05-08 (1 year ago) RSS 2.0 feedNot yet rated by the usersTotal: 135 All time: 8,501 This week: 636Up
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
  Performance   Level  
Name: Alex F. Bustos <contact>
Classes: 1 package by
Country: Colombia Colombia
Age: 22
All time rank: 400829 in Colombia Colombia
Week rank: 980 Up10 in Colombia Colombia Up
Innovation award
Innovation award
Nominee: 1x

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.


1. Include, instantiate and (opt) set the numeric and list limitations
	include(''); //Maybe you want to change some default values
	$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

3. (opt) Insert Additive Rules

4.	$sci->start(); //the object.
Else, the main functions will return the unmodified value


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.

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
set_charset($charset) - Valid Charset. See the $encoding arg of 

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();
... //Maybe you wish to add some rules
  Example includedExternal page  

Open in a separate window

  • 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 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  
This week:0
All time:8,501
This week:636Up