PHP Classes
elePHPant
Icontem

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

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Info   View files Documentation   Demos   View files View files (11)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings  
2014-10-16 (10 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 124 All time: 7,901 This week: 1,201Up
Version License PHP version Categories  
sci-convert 1.1GNU General Publi...5.3PHP 5, Conversion, Math
Description Author  

This class can convert a number to a string appending a prefix.

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-)

Puede especificar una base personalizada y su lista de prefijos asociados, guardando dicha lista en un fichero que se añade al directorio de prefijos.

Convirtiendo de numero a formato con prefijos, obtiene el numero transformado (parte numérica) y uno de los prefijos (prefijo) para el sistema seleccionado.

Se puede escoger el modo de redondeo de la parte numérica, si se desea obtener el resultado en digitos significativos o en posiciones decimales determinadas.

Se puede escoger si se desean ambas partes (numerica y prefijo) unidas en un string, o cada una por aparte en un arreglo, con la intención de que cada una se pueda formatear independientemente (¿Quizas el prefijo deba de estar en negrilla y rojo?)

Convirtiendo de representación con prefijos a numero, la clase permite usar caracteres especiales (ej, los caracteres "micro" y "mu") habiendo especificado la codificación correcta previamente.

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
Name: Alex F. Bustos <contact>
Classes: 1 package by
Country: Colombia Colombia
Age: 21
All time rank: 382126 in Colombia Colombia
Week rank: 1497 Up11 in Colombia Colombia Up
Innovation award
Innovation award
Nominee: 1x

Details provided by the author  
File: readme.txt
Created by: Alex Bustos - @alexfbp

At first, Thanks for downloading and testing it.

That file are intented to be as a resume and a little explanation of how to use
this class. For more detailed info, see the class an examples 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 you are using the first arg. of the construct as a variable, you are directly
responsible to clean that variable from forbidden expressions, such as "..", "/"...
The 'LOP' are the DESIRED list, the final list used by the class will be generated
with a call to start() - 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  
  Files folder image Files  
File Role Description
Files folder imagesets (7 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_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

 Version Control Unique User Downloads Download Rankings  
 100%Total:124All time:7,901
 This week:0This week:1,201Up