How to make number_format() not to round numbers up - php

I have this number:
$double = '21.188624';
After using number_format($double, 2, ',', ' ') I get:
21,19
But what I want is:
21,18
Any ideea how can I make this work?
Thank you.

number_format will always do that, your only solution is to feed it something different:
$number = intval(($number*100))/100;
Or:
$number = floor(($number*100))/100;

I know that this an old question, but it still actual :) .
How about this function?
function numberFormatPrecision($number, $precision = 2, $separator = '.')
{
$numberParts = explode($separator, $number);
$response = $numberParts[0];
if (count($numberParts)>1 && $precision > 0) {
$response .= $separator;
$response .= substr($numberParts[1], 0, $precision);
}
return $response;
}
Usage:
// numbers test
numberFormatPrecision(19, 2, '.'); // expected 19 return 19
numberFormatPrecision(19.1, 2, '.'); //expected 19.1 return 19.1
numberFormatPrecision(19.123456, 2, '.'); //expected 19.12 return 19.12
numberFormatPrecision(19.123456, 0, '.'); //expected 19 return 19
// negative numbers test
numberFormatPrecision(-19, 2, '.'); // expected -19 return -19
numberFormatPrecision(-19.1, 2, '.'); //expected -19.1 return -19.1
numberFormatPrecision(-19.123456, 2, '.'); //expected -19.12 return -19.12
numberFormatPrecision(-19.123456, 0, '.'); //expected -19 return -19
// precision test
numberFormatPrecision(-19.123456, 4, '.'); //expected -19.1234 return -19.1234
// separator test
numberFormatPrecision('-19,123456', 3, ','); //expected -19,123 return -19,123 -- comma separator

Function (only precision):
function numberPrecision($number, $decimals = 0)
{
$negation = ($number < 0) ? (-1) : 1;
$coefficient = 10 ** $decimals;
return $negation * floor((string)(abs($number) * $coefficient)) / $coefficient;
}
Examples:
numberPrecision(2557.9999, 2); // returns 2557.99
numberPrecision(2557.9999, 10); // returns 2557.9999
numberPrecision(2557.9999, 0); // returns 2557
numberPrecision(2557.9999, -2); // returns 2500
numberPrecision(2557.9999, -10); // returns 0
numberPrecision(-2557.9999, 2); // returns -2557.99
numberPrecision(-2557.9999, 10); // returns -2557.9999
numberPrecision(-2557.9999, 0); // returns -2557
numberPrecision(-2557.9999, -2); // returns -2500
numberPrecision(-2557.9999, -10); // returns 0
Function (full functionality):
function numberFormat($number, $decimals = 0, $decPoint = '.' , $thousandsSep = ',')
{
$negation = ($number < 0) ? (-1) : 1;
$coefficient = 10 ** $decimals;
$number = $negation * floor((string)(abs($number) * $coefficient)) / $coefficient;
return number_format($number, $decimals, $decPoint, $thousandsSep);
}
Examples:
numberFormat(2557.9999, 2, ',', ' '); // returns 2 557,99
numberFormat(2557.9999, 10, ',', ' '); // returns 2 557,9999000000
numberFormat(2557.9999, 0, ',', ' '); // returns 2 557
numberFormat(2557.9999, -2, ',', ' '); // returns 2 500
numberFormat(2557.9999, -10, ',', ' '); // returns 0
numberFormat(-2557.9999, 2, ',', ' '); // returns -2 557,99
numberFormat(-2557.9999, 10, ',', ' '); // returns -2 557,9999000000
numberFormat(-2557.9999, 0, ',', ' '); // returns -2 557
numberFormat(-2557.9999, -2, ',', ' '); // returns -2 500
numberFormat(-2557.9999, -10, ',', ' '); // returns 0

floor($double*100)/100

I use this function:
function cutNum($num, $precision = 2) {
return floor($num) . substr(str_replace(floor($num), '', $num), 0, $precision + 1);
}
Usage examples:
cutNum(5) //returns 5
cutNum(5.6789) //returns 5.67 (default precision is two decimals)
cutNum(5.6789, 3) //returns 5.678
cutNum(5.6789, 10) //returns 5.6789
cutNum(5.6789, 0) //returns 5. (!don't use with zero as second argument: use floor instead!)
Explanation: here you have the same function, just more verbose to help understanding its behaviour:
function cutNum($num, $precision = 2) {
$integerPart = floor($num);
$decimalPart = str_replace($integerPart, '', $num);
$trimmedDecimal = substr($decimalPart, 0, $precision + 1);
return $integerPart . $trimmedDecimal;
}

Use the PHP native function bcdiv.
function numberFormat($number, $decimals = 2, $sep = ".", $k = ","){
$number = bcdiv($number, 1, $decimals); // Truncate decimals without rounding
return number_format($number, $decimals, $sep, $k); // Format the number
}
See this answer for more details.

**Number without round**
$double = '21.188624';
echo intval($double).'.'.substr(end(explode('.',$double)),0,2);
**Output** 21.18

In case you don't care for what comes behind the decimal point, you can cast the float as an int to avoid rounding:
$float = 2.8;
echo (int) $float; // outputs '2'

$double = '21.188624';
$teX = explode('.', $double);
if(isset($teX[1])){
$de = substr($teX[1], 0, 2);
$final = $teX[0].'.'.$de;
$final = (float) $final;
}else{
$final = $double;
}
final will be 21.18

In case you need 2 fixed decimal places, you can try this!
#Dima's solution is working for me, but it prints "19.90" as "19.9" so I made some changes as follows:
<?php
function numberPrecision($number, $decimals = 0)
{
$negation = ($number < 0) ? (-1) : 1;
$coefficient = 10 ** $decimals;
$result = $negation * floor((string)(abs($number) * $coefficient)) / $coefficient;
$arr = explode(".", $result);
$num = $arr[0];
if(empty($arr[1]))
$num .= ".00";
else if(strlen($arr[1]) == 1)
$num .= "." . $arr[1] . "0";
else
$num .= ".". $arr[1];
return $num;
}
echo numberPrecision(19.90,2); // 19.90
So, what I did is, I just break the result into two parts with explode function. and convert the result into a string with concatenation!

public function numberFormatPrecision( $number, $separator = '.', $format = 2 ){
$response = '';
$brokenNumber = explode( $separator, $number );
$response = $brokenNumber[0] . $separator;
$brokenBackNumber = str_split($brokenNumber[1]);
if( $format < count($brokenBackNumber) ){
for( $i = 1; $i <= $format; $i++ )
$response .= $brokenBackNumber[$i];
}
return $response;
}

$finalCommishParts = explode('.',$commission);
$commisshSuffix = (isset($finalCommishParts[1])?substr($finalCommishParts[1],0,2):'00');
$finalCommish = $finalCommishParts[0].'.'.$commisshSuffix;

The faster way as exploding(building arrays) is to do it with string commands like this:
$number = ABC.EDFG;
$precision = substr($number, strpos($number, '.'), 3); // 3 because . plus 2 precision
$new_number = substr($number, 0, strpos($number, '.')).$precision;
The result ist ABC.ED in this case because of 2 precision
If you want more precision just change the 3 to 4 or X to have X-1 precision
Cheers

Javascript Version
function numberFormat($number, $decimals = 0, $decPoint = '.' , $thousandsSep = ',')
{
return number_format((Math.floor($number * 100) / 100).toFixed($decimals), $decimals, $decPoint, $thousandsSep );
}
// https://locutus.io/php/strings/number_format/
function number_format(number, decimals, decPoint, thousandsSep) {
if(decimals === 'undefined') decimals = 2;
number = (number + '').replace(/[^0-9+\-Ee.]/g, '')
const n = !isFinite(+number) ? 0 : +number
const prec = !isFinite(+decimals) ? 0 : Math.abs(decimals)
const sep = (typeof thousandsSep === 'undefined') ? ',' : thousandsSep
const dec = (typeof decPoint === 'undefined') ? '.' : decPoint
let s = ''
const toFixedFix = function (n, prec) {
if (('' + n).indexOf('e') === -1) {
return +(Math.round(n + 'e+' + prec) + 'e-' + prec)
} else {
const arr = ('' + n).split('e')
let sig = ''
if (+arr[1] + prec > 0) {
sig = '+'
}
return (+(Math.round(+arr[0] + 'e' + sig + (+arr[1] + prec)) + 'e-' + prec)).toFixed(prec)
}
}
// #todo: for IE parseFloat(0.55).toFixed(0) = 0;
s = (prec ? toFixedFix(n, prec).toString() : '' + Math.round(n)).split('.')
if (s[0].length > 3) {
s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep)
}
if ((s[1] || '').length < prec) {
s[1] = s[1] || ''
s[1] += new Array(prec - s[1].length + 1).join('0')
}
return s.join(dec)
}

$number = 2.278;
echo new_number_format($number,1);
//result: 2.2
function new_number_format($number,$decimal)
{
//explode the number with the delimiter of dot(.) and get the whole number in index 0 and the decimal in index 1
$num = explode('.',$number);
//if the decimal is equal to zero
//take note that we can't split the zero value only and it will return Undefined offset if we split the zero only
//for example: rating_format(2.0,1); the result will be 2. the zero is gone because of the Undefined offset
//the solution of this problem is this condition below
if($num[1] == 0)
{
$final_decimal = '';
$i=0;
//loop the decimal so that we can display depend on how many decimal that you want to display
while($i<$decimal){
$final_decimal .= 0;
$i++;
}
}
//if the decimal is not zero
else
{
$dec = str_split($num[1]); //split the decimal and get the value using the array index
$i=0;
$final_decimal = '';
//loop the decimal so that we can display depend on how many decimal that you want to display
while($i<$decimal){
$final_decimal .= $dec[$i];
$i++;
}
}
$new_number= $num[0].'.'.$final_decimal;//combine the result with final decimal
return $new_number; //return the final output
}

thanks for your help Dima!!! My function
private function number_format(float $num, int $decimals = 0, ?string $decimal_separator = ',', ?string $thousands_separator = '.'){
/**
* Formatea un numero como number_format sin redondear hacia arriba, trunca el resultado
* #access private
* #param string num - Numero de va a ser formateado
* #param int decimals - Posiciones Decimales
* #param string|null $decimal_separator — [opcional]
* #param string|null $thousands_separator — [opcional]
* #return string — Version de numero formateado.
*/
$negation = ($num < 0) ? (-1) : 1;
$coefficient = 10 ** $decimals;
$number = $negation * floor((string)(abs($num) * $coefficient)) / $coefficient;
return number_format($number, $decimals, $decimal_separator, $thousands_separator);
}
for use it
echo $this->number_format(24996.46783, 3, ',', '.'); //24.996,467

use this function:
function number_format_unlimited_precision($number,$decimal = '.')
{
$broken_number = explode($decimal,$number);
return number_format($broken_number[0]).$decimal.$broken_number[1]);
}

In Case you have small float values you can use number_format function this way.
$number = 21.23;
echo number_format($number, 2, '.', ',') ); // 21.23
In case you have you have long decimal number then also it will format number this way
$number = 201541.23;
echo number_format($number, 2, '.', ',') ); // 201,541.23

Related

how to change the color of decimals points in php

how can I change the colors of ONLY decimals of a number in PHP?
this is my function for formatting numbers
function formatNumber($input, $decimals = 'auto', $prefix = '', $suffix = '') {
$input = floatval($input);
$absInput = abs($input);
if ($decimals === 'auto') {
if ($absInput >= 0.01) {
$decimals = 2;
} elseif (0.0001 <= $absInput && $absInput < 0.01) {
$decimals = 4;
} elseif (0.000001 <= $absInput && $absInput < 0.0001) {
$decimals = 6;
} elseif ($absInput < 0.000001) {
$decimals = 8;
}
}
if($input>1000000000000000){
$result = ROUND(($input/1000000000000000),2).' TH ';
}elseif($input>1000000000000){
$result = ROUND(($input/1000000000000),2).' T ';
}elseif($input>1000000000){
$result = ROUND(($input/1000000000),2).' B ';
}elseif($input>1000000) {
$result = ROUND(($input / 1000000), 2) . ' M ';
} else {
$result = number_format($input, $decimals, config('decimal-separator','.'), config('thousand-separator', ',')) ;
}
return ($prefix ? $prefix : '') . $result. ($suffix ? $suffix : '');
}
and I use it like that
<?php echo formatNumber($chart['assist'], 2)?>
i want my decimals with a different color... can i use css there or add classes?
Here is an example of what I meant in my comment by manipulate the string:
<?php
$n = 123.456;
$whole = floor($n); // 123
$fraction = $n - $whole; // .456
//echo str_replace('.', '<span class="colorme">.</span>', $n);
echo $whole . '<span class="colorme">.</span>' . substr($fraction, strpos($fraction, '.')+1);
//Simply do a string replace on the decimal point.
UPDATED break out parts, concatenate.
A client side approach with Javascript (with some jQuery) would be something like:
$('#myDiv').each(function () {
$(this).html($(this).html().replace(/\./g, '<span class="colorme">.</span>'));
//or decimal point and decimal number part...
$(this).html($(this).html().replace(/\.([0-9]+)/g, '<span class="colorme">.$1</span>'));
});
Remember that other locales don't always use . for divider.
So with your existing code, you could do something like:
$dec_point = config('decimal-separator','.');
$wrapped_dec_point = "<span class='dec_point'>{$dec_point}</span>";
$result = number_format($input, $decimals, $wrapped_dec_point, config('thousand-separator', ',')) ;
and then of course, for your CSS, you would just need
.dec_point {
color: magenta;
}
Here is shorter solution
$n = 123.456;
$nums = explode(".",$n);
echo $nums[0] . '<span class="colorme">.' . $nums[1] . '</span>';

php calculate string values: "2+4+3-12+3-5"

How can I calculate values in a string containing the following numbers and (+/-) operators:
Code Like
$string = "3+5+3-7+4-3-1";
//result should be = 4
Updated:
I am trying to calculate $array = [1, +, 6, -, 43, +, 10];
I have converted into the string: implode("", $array);
you can use eval
$string = "3+5+3-7+4-3-1";
eval( '$res = (' . $string . ');' );
echo $res;
$arr_val = array(1, '+', 6, '-', 43, '+', 10);
$total = 0;
if(isset($arr_val[0]) && ($arr_val[0] != '+' || $arr_val[0] != '-'))
$total = intval($arr_val[0]);
foreach($arr_val AS $key => $val) {
if($val == '+') {
if(isset($arr_val[$key+1]) && ($arr_val[$key+1] != '+' || $arr_val[$key+1] != '-')) {
$total = $total + intval($arr_val[$key+1]);
}
} else if($val == '-') {
if(isset($arr_val[$key+1]) && ($arr_val[$key+1] != '+' || $arr_val[$key+1] != '-')) {
$total = $total - intval($arr_val[$key+1]);
}
}
}
echo $total;
May be it will solve your problem.
For any kind of array like: [1, + , 4, -, 5, , 3, 8, + , 6]
Solved with the custom php helper function:
function calcArray($arrVal)
{
if (count($arrVal) == 1) {
return reset($arrVal);
}
if (is_int($arrVal[1])) {
$arrVal[0] = $arrVal[0].$arrVal[1];
unset($arrVal[1]);
return calcArray(array_values($arrVal));
}
$emptyValKey = array_search('', $arrVal);
if ($emptyValKey) {
$concatVal = $arrVal[$emptyValKey-1].$arrVal[$emptyValKey+1];
unset($arrVal[$emptyValKey+1]);
unset($arrVal[$emptyValKey]);
$arrVal[$emptyValKey-1] = $concatVal;
return calcArray(array_values($arrVal));
}
$total = $arrVal[1] == "+" ? $arrVal[0] + $arrVal[2]:$arrVal[0] - $arrVal[2];
unset($arrVal[0]);
unset($arrVal[1]);
unset($arrVal[2]);
array_unshift($arrVal, $total);
$arrVal = array_values(array_filter($arrVal));
return calcArray($arrVal);
}

Number formatting <-

So I have a website video like Youtube and the problem is that
I want the number like this exemple :
1,234 views -> 1,2 K
So this is the code
function pm_number_format($number, $decimals = 0, $dec_point = '.', $thousands_sep = ',')
{
return number_format($number, $decimals, $dec_point, $thousands_sep);
}
function pm_compact_number_format($number)
{
if ($number < 10000)
{
return pm_number_format($number);
}
$d = $number < 1000000 ? 1000 : 1000000;
$f = round($number / $d, 1);
return pm_number_format($f, $f - intval($f) ? 1 : 0) . ($d == 1000 ? 'k' : 'M');
}
Just change your if condition and remove 1x zero, so from this:
if ($number < 10000) {
return pm_number_format($number);
}
to this:
if ($number < 1000) {
return pm_number_format($number);
}
Input:
1
12
123
1234
12345
123456
1234567
12345678
123456789
Output:
1
12
123
1.2K //<--See output as you wanted
12.3K
123.5K
1.2M
12.3M
123.5M
EDIT:
Here is my code i modify(like i described up above) and used to produce the output:
<?php
function pm_number_format($number, $decimals = 0, $dec_point = '.', $thousands_sep = ',') {
return number_format($number, $decimals, $dec_point, $thousands_sep);
}
function pm_compact_number_format($number) {
if ($number < 1000)
return pm_number_format($number);
$d = $number < 1000000 ? 1000 : 1000000;
$f = round($number / $d, 1);
return pm_number_format($f, $f - intval($f) ? 1 : 0) . ($d == 1000 ? 'k' : 'M');
}
$number = "";
foreach(range(1,10) as $value) {
$number .= $value;
echo pm_compact_number_format($number) . "<br />";
}
?>

Converting float decimal to fraction

I am trying to convert calculations keyed in by users with decimal results into fractions. For e.g.; 66.6666666667 into 66 2/3. Any pointers?
Thanx in advance
Continued fractions can be used to find rational approximations to real numbers that are "best" in a strict sense. Here's a PHP function that finds a rational approximation to a given (positive) floating point number with a relative error less than $tolerance:
<?php
function float2rat($n, $tolerance = 1.e-6) {
$h1=1; $h2=0;
$k1=0; $k2=1;
$b = 1/$n;
do {
$b = 1/$b;
$a = floor($b);
$aux = $h1; $h1 = $a*$h1+$h2; $h2 = $aux;
$aux = $k1; $k1 = $a*$k1+$k2; $k2 = $aux;
$b = $b-$a;
} while (abs($n-$h1/$k1) > $n*$tolerance);
return "$h1/$k1";
}
printf("%s\n", float2rat(66.66667)); # 200/3
printf("%s\n", float2rat(sqrt(2))); # 1393/985
printf("%s\n", float2rat(0.43212)); # 748/1731
I have written more about this algorithm and why it works, and even a JavaScript demo here: https://web.archive.org/web/20180731235708/http://jonisalonen.com/2012/converting-decimal-numbers-to-ratios/
Farey fractions can be quite useful in this case.
They can be used to convert any decimal into a fraction with the lowest possible denominator.
Sorry - I don't have a prototype in PHP, so here's one in Python:
def farey(v, lim):
"""No error checking on args. lim = maximum denominator.
Results are (numerator, denominator); (1, 0) is 'infinity'."""
if v < 0:
n, d = farey(-v, lim)
return (-n, d)
z = lim - lim # Get a "zero of the right type" for the denominator
lower, upper = (z, z+1), (z+1, z)
while True:
mediant = (lower[0] + upper[0]), (lower[1] + upper[1])
if v * mediant[1] > mediant[0]:
if lim < mediant[1]:
return upper
lower = mediant
elif v * mediant[1] == mediant[0]:
if lim >= mediant[1]:
return mediant
if lower[1] < upper[1]:
return lower
return upper
else:
if lim < mediant[1]:
return lower
upper = mediant
Converted Python code in answer from #APerson241 to PHP
<?php
function farey($v, $lim) {
// No error checking on args. lim = maximum denominator.
// Results are array(numerator, denominator); array(1, 0) is 'infinity'.
if($v < 0) {
list($n, $d) = farey(-$v, $lim);
return array(-$n, $d);
}
$z = $lim - $lim; // Get a "zero of the right type" for the denominator
list($lower, $upper) = array(array($z, $z+1), array($z+1, $z));
while(true) {
$mediant = array(($lower[0] + $upper[0]), ($lower[1] + $upper[1]));
if($v * $mediant[1] > $mediant[0]) {
if($lim < $mediant[1])
return $upper;
$lower = $mediant;
}
else if($v * $mediant[1] == $mediant[0]) {
if($lim >= $mediant[1])
return $mediant;
if($lower[1] < $upper[1])
return $lower;
return $upper;
}
else {
if($lim < $mediant[1])
return $lower;
$upper = $mediant;
}
}
}
// Example use:
$f = farey(66.66667, 10);
echo $f[0], '/', $f[1], "\n"; # 200/3
$f = farey(sqrt(2), 1000);
echo $f[0], '/', $f[1], "\n"; # 1393/985
$f = farey(0.43212, 2000);
echo $f[0], '/', $f[1], "\n"; # 748/1731
Based upon #Joni's answer, here is what I used to pull out the whole number.
function convert_decimal_to_fraction($decimal){
$big_fraction = float2rat($decimal);
$num_array = explode('/', $big_fraction);
$numerator = $num_array[0];
$denominator = $num_array[1];
$whole_number = floor( $numerator / $denominator );
$numerator = $numerator % $denominator;
if($numerator == 0){
return $whole_number;
}else if ($whole_number == 0){
return $numerator . '/' . $denominator;
}else{
return $whole_number . ' ' . $numerator . '/' . $denominator;
}
}
function float2rat($n, $tolerance = 1.e-6) {
$h1=1; $h2=0;
$k1=0; $k2=1;
$b = 1/$n;
do {
$b = 1/$b;
$a = floor($b);
$aux = $h1; $h1 = $a*$h1+$h2; $h2 = $aux;
$aux = $k1; $k1 = $a*$k1+$k2; $k2 = $aux;
$b = $b-$a;
} while (abs($n-$h1/$k1) > $n*$tolerance);
return "$h1/$k1";
}
Based on #APerson's and #Jeff Monteiro's answers I've created PHP version of Farey fractions that will be simplified to whole values with fractions with lowest possible denominator:
<?php
class QuantityTransform
{
/**
* #see https://stackoverflow.com/questions/14330713/converting-float-decimal-to-fraction
*/
public static function decimalToFraction(float $decimal, $glue = ' ', int $limes = 10): string
{
if (null === $decimal || $decimal < 0.001) {
return '';
}
$wholeNumber = (int) floor($decimal);
$remainingDecimal = $decimal - $wholeNumber;
[$numerator, $denominator] = self::fareyFraction($remainingDecimal, $limes);
// Values rounded to 1 should be added to base value and returned without fraction part
if (is_int($simplifiedFraction = $numerator / $denominator)) {
$wholeNumber += $simplifiedFraction;
$numerator = 0;
}
return (0 === $wholeNumber && 0 === $numerator)
// Too small values will be returned in original format
? (string) $decimal
// Otherwise let's format value - only non-0 whole value / fractions will be returned
: trim(sprintf(
'%s%s%s',
(string) $wholeNumber ?: '',
$wholeNumber > 0 ? $glue : '',
0 === $numerator ? '' : ($numerator . '/' . $denominator)
));
}
/**
* #see https://stackoverflow.com/a/14330799/842480
*
* #return int[] Numerator and Denominator values
*/
private static function fareyFraction(float $value, int $limes): array
{
if ($value < 0) {
[$numerator, $denominator] = self::fareyFraction(-$value, $limes);
return [-$numerator, $denominator];
}
$zero = $limes - $limes;
$lower = [$zero, $zero + 1];
$upper = [$zero + 1, $zero];
while (true) {
$mediant = [$lower[0] + $upper[0], $lower[1] + $upper[1]];
if ($value * $mediant[1] > $mediant[0]) {
if ($limes < $mediant[1]) {
return $upper;
}
$lower = $mediant;
} elseif ($value * $mediant[1] === $mediant[0]) {
if ($limes >= $mediant[1]) {
return $mediant;
}
if ($lower[1] < $upper[1]) {
return $lower;
}
return $upper;
} else {
if ($limes < $mediant[1]) {
return $lower;
}
$upper = $mediant;
}
}
}
}
Then you san use it like:
QuantityTransform::decimalToFraction(0.06); // 0.06
QuantityTransform::decimalToFraction(0.75); // 3/4
QuantityTransform::decimalToFraction(1.75, ' and '); // 1 and 3/4
QuantityTransform::decimalToFraction(2.33, ' and '); // 2 and 1/3
QuantityTransform::decimalToFraction(2.58, ' ', 5); // 2 3/5
QuantityTransform::decimalToFraction(2.58, ' & ', 10); // 2 & 4/7
QuantityTransform::decimalToFraction(1.97); // 2
Here is my approach to this problem. Works fine with rational numbers.
function dec2fracso($dec){
//Negative number flag.
$num=$dec;
if($num<0){
$neg=true;
}else{
$neg=false;
}
//Extracts 2 strings from input number
$decarr=explode('.',(string)$dec);
//Checks for divided by zero input.
if($decarr[1]==0){
$decarr[1]=1;
$fraccion[0]=$decarr[0];
$fraccion[1]=$decarr[1];
return $fraccion;
}
//Calculates the divisor before simplification.
$long=strlen($decarr[1]);
$div="1";
for($x=0;$x<$long;$x++){
$div.="0";
}
//Gets the greatest common divisor.
$x=(int)$decarr[1];
$y=(int)$div;
$gcd=gmp_strval(gmp_gcd($x,$y));
//Calculates the result and fills the array with the correct sign.
if($neg){
$fraccion[0]=((abs($decarr[0])*($y/$gcd))+($x/$gcd))*(-1);
}else{
$fraccion[0]=(abs($decarr[0])*($y/$gcd))+($x/$gcd);
}
$fraccion[1]=($y/$gcd);
return $fraccion;
}
Sometimes it is necessary to treat only the decimals of a float. So I created a code that uses the function created by #Joni to present a format that is quite common in culinary recipes, at least in Brazil.
So instead of using 3/2 which is the result for 1.5, using the function I created it is possible to present the value 1 1/2, and if you want, you can also add a string to concatenate the values, creating something like "1 and 1/2 ".
function float2rat($n, $tolerance = 1.e-6) {
$h1=1; $h2=0;
$k1=0; $k2=1;
$b = 1/$n;
do {
$b = 1/$b;
$a = floor($b);
$aux = $h1; $h1 = $a*$h1+$h2; $h2 = $aux;
$aux = $k1; $k1 = $a*$k1+$k2; $k2 = $aux;
$b = $b-$a;
} while (abs($n-$h1/$k1) > $n*$tolerance);
return "$h1/$k1";
}
function float2fraction($float, $concat = ' '){
// ensures that the number is float,
// even when the parameter is a string
$float = (float)$float;
if($float == 0 ){
return $float;
}
// when float between -1 and 1
if( $float > -1 && $float < 0 || $float < 1 && $float > 0 ){
$fraction = float2rat($float);
return $fraction;
}
else{
// get the minor integer
if( $float < 0 ){
$integer = ceil($float);
}
else{
$integer = floor($float);
}
// get the decimal
$decimal = $float - $integer;
if( $decimal != 0 ){
$fraction = float2rat(abs($decimal));
$fraction = $integer . $concat . $fraction;
return $fraction;
}
else{
return $float;
}
}
}
Usage e.g:
echo float2fraction(1.5);
will return "1 1/2"

Format bytes to kilobytes, megabytes, gigabytes

Scenario: the size of various files are stored in a database as bytes. What's the best way to format this size info to kilobytes, megabytes and gigabytes? For instance I have an MP3 that Ubuntu displays as "5.2 MB (5445632 bytes)". How would I display this on a web page as "5.2 MB" AND have files less than one megabyte display as KB and files one gigabyte and above display as GB?
function formatBytes($bytes, $precision = 2) {
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
// Uncomment one of the following alternatives
// $bytes /= pow(1024, $pow);
// $bytes /= (1 << (10 * $pow));
return round($bytes, $precision) . ' ' . $units[$pow];
}
(Taken from php.net, there are many other examples there, but I like this one best :-)
This is Chris Jester-Young's implementation, cleanest I've ever seen, combined with php.net's and a precision argument.
function formatBytes($size, $precision = 2)
{
$base = log($size, 1024);
$suffixes = array('', 'K', 'M', 'G', 'T');
return round(pow(1024, $base - floor($base)), $precision) .' '. $suffixes[floor($base)];
}
echo formatBytes(24962496);
// 23.81M
echo formatBytes(24962496, 0);
// 24M
echo formatBytes(24962496, 4);
// 23.8061M
Pseudocode:
$base = log($size) / log(1024);
$suffix = array("", "k", "M", "G", "T")[floor($base)];
return pow(1024, $base - floor($base)) . $suffix;
Just divide it by 1024 for kb, 1024^2 for mb and 1024^3 for GB. As simple as that.
This is Kohana's implementation, you could use it:
public static function bytes($bytes, $force_unit = NULL, $format = NULL, $si = TRUE)
{
// Format string
$format = ($format === NULL) ? '%01.2f %s' : (string) $format;
// IEC prefixes (binary)
if ($si == FALSE OR strpos($force_unit, 'i') !== FALSE)
{
$units = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB');
$mod = 1024;
}
// SI prefixes (decimal)
else
{
$units = array('B', 'kB', 'MB', 'GB', 'TB', 'PB');
$mod = 1000;
}
// Determine unit to use
if (($power = array_search((string) $force_unit, $units)) === FALSE)
{
$power = ($bytes > 0) ? floor(log($bytes, $mod)) : 0;
}
return sprintf($format, $bytes / pow($mod, $power), $units[$power]);
}
use this function if you want a short code
bcdiv()
$size = 11485760;
echo bcdiv($size, 1048576, 0); // return: 10
echo bcdiv($size, 1048576, 2); // return: 10,9
echo bcdiv($size, 1048576, 2); // return: 10,95
echo bcdiv($size, 1048576, 3); // return: 10,953
Just my alternative, short and clean:
/**
* #param int $bytes Number of bytes (eg. 25907)
* #param int $precision [optional] Number of digits after the decimal point (eg. 1)
* #return string Value converted with unit (eg. 25.3KB)
*/
function formatBytes($bytes, $precision = 2) {
$unit = ["B", "KB", "MB", "GB"];
$exp = floor(log($bytes, 1024)) | 0;
return round($bytes / (pow(1024, $exp)), $precision).$unit[$exp];
}
or, more stupid and efficent:
function formatBytes($bytes, $precision = 2) {
if ($bytes > pow(1024,3)) return round($bytes / pow(1024,3), $precision)."GB";
else if ($bytes > pow(1024,2)) return round($bytes / pow(1024,2), $precision)."MB";
else if ($bytes > 1024) return round($bytes / 1024, $precision)."KB";
else return ($bytes)."B";
}
I know it's maybe a little late to answer this question but, more data is not going to kill someone. Here's a very fast function :
function format_filesize($B, $D=2){
$S = 'BkMGTPEZY';
$F = floor((strlen($B) - 1) / 3);
return sprintf("%.{$D}f", $B/pow(1024, $F)).' '.#$S[$F].'B';
}
EDIT: I updated my post to include the fix proposed by camomileCase:
function format_filesize($B, $D=2){
$S = 'kMGTPEZY';
$F = floor((strlen($B) - 1) / 3);
return sprintf("%.{$D}f", $B/pow(1024, $F)).' '.#$S[$F-1].'B';
}
Simple function
function formatBytes($size, $precision = 0){
$unit = ['Byte','KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB'];
for($i = 0; $size >= 1024 && $i < count($unit)-1; $i++){
$size /= 1024;
}
return round($size, $precision).' '.$unit[$i];
}
echo formatBytes('1876144', 2);
//returns 1.79 MiB
Extremely simple function to get human file size.
Original source: http://php.net/manual/de/function.filesize.php#106569
Copy/paste code:
<?php
function human_filesize($bytes, $decimals = 2) {
$sz = 'BKMGTP';
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . #$sz[$factor];
}
?>
function convertToReadableSize($size)
{
$base = log($size) / log(1024);
$suffix = array("B", "KB", "MB", "GB", "TB");
$f_base = floor($base);
return round(pow(1024, $base - floor($base)), 1) . $suffix[$f_base];
}
Just call the function
echo convertToReadableSize(1024); // Outputs '1KB'
echo convertToReadableSize(1024 * 1024); // Outputs '1MB'
Flexible solution:
function size($size, array $options=null) {
$o = [
'binary' => false,
'decimalPlaces' => 2,
'decimalSeparator' => '.',
'thausandsSeparator' => '',
'maxThreshold' => false, // or thresholds key
'suffix' => [
'thresholds' => ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'],
'decimal' => ' {threshold}B',
'binary' => ' {threshold}iB',
'bytes' => ' B'
]
];
if ($options !== null)
$o = array_replace_recursive($o, $options);
$base = $o['binary'] ? 1024 : 1000;
$exp = $size ? floor(log($size) / log($base)) : 0;
if (($o['maxThreshold'] !== false) &&
($o['maxThreshold'] < $exp)
)
$exp = $o['maxThreshold'];
return !$exp
? (round($size) . $o['suffix']['bytes'])
: (
number_format(
$size / pow($base, $exp),
$o['decimalPlaces'],
$o['decimalSeparator'],
$o['thausandsSeparator']
) .
str_replace(
'{threshold}',
$o['suffix']['thresholds'][$exp],
$o['suffix'][$o['binary'] ? 'binary' : 'decimal']
)
);
}
var_dump(size(disk_free_space('/')));
// string(8) "14.63 GB"
var_dump(size(disk_free_space('/'), ['binary' => true]));
// string(9) "13.63 GiB"
var_dump(size(disk_free_space('/'), ['maxThreshold' => 2]));
// string(11) "14631.90 MB"
var_dump(size(disk_free_space('/'), ['binary' => true, 'maxThreshold' => 2]));
// string(12) "13954.07 MiB"
My approach
function file_format_size($bytes, $decimals = 2) {
$unit_list = array('B', 'KB', 'MB', 'GB', 'PB');
if ($bytes == 0) {
return $bytes . ' ' . $unit_list[0];
}
$unit_count = count($unit_list);
for ($i = $unit_count - 1; $i >= 0; $i--) {
$power = $i * 10;
if (($bytes >> $power) >= 1)
return round($bytes / (1 << $power), $decimals) . ' ' . $unit_list[$i];
}
}
I don't know why you should make it so complicated as the others.
The following code is much simpler to understand and about 25% faster than the other solutions who uses the log function (called the function 20 Mio. times with different parameters)
function formatBytes($bytes, $precision = 2) {
$units = ['Byte', 'Kilobyte', 'Megabyte', 'Gigabyte', 'Terabyte'];
$i = 0;
while($bytes > 1024) {
$bytes /= 1024;
$i++;
}
return round($bytes, $precision) . ' ' . $units[$i];
}
Here is an option using log10:
<?php
function format_number(float $d): string {
$e = (int)(log10($d) / 3);
return sprintf('%.3f', $d / 1e3 ** $e) . ['', ' k', ' M', ' G'][$e];
}
$s = format_number(9012345678);
var_dump($s == '9.012 G');
https://php.net/function.log10
My own implementation for getting formatted file size from integer size. Simple to understand and easy to extend to accommodate larger files - Just follow the pattern.
<?php
function getFormattedFileSize($size, $precision)
{
switch (true)
{
case ($size/1024 < 1):
return $size.'B';
case ($size/pow(1024, 2) < 1):
return round($size/1024, $precision).'KB';
case ($size/pow(1024, 3) < 1):
return round($size/pow(1024, 2), $precision).'MB';
case ($size/pow(1024, 4) < 1):
return round($size/pow(1024, 3), $precision).'GB';
case ($size/pow(1024, 5) < 1):
return round($size/pow(1024, 4), $precision).'TB';
default:
return 'Error: invalid input or file is too large.';
}
}
I succeeded with following function,
function format_size($size) {
$mod = 1024;
$units = explode(' ','B KB MB GB TB PB');
for ($i = 0; $size > $mod; $i++) {
$size /= $mod;
}
return round($size, 2) . ' ' . $units[$i];
}
try this ;)
function bytesToSize($bytes) {
$sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if ($bytes == 0) return 'n/a';
$i = intval(floor(log($bytes) / log(1024)));
if ($i == 0) return $bytes . ' ' . $sizes[$i];
return round(($bytes / pow(1024, $i)),1,PHP_ROUND_HALF_UP). ' ' . $sizes[$i];
}
echo bytesToSize(10000050300);
function changeType($size, $type, $end){
$arr = ['B', 'KB', 'MB', 'GB', 'TB'];
$tSayi = array_search($type, $arr);
$eSayi = array_search($end, $arr);
$pow = $eSayi - $tSayi;
return $size * pow(1024 * $pow) . ' ' . $end;
}
echo changeType(500, 'B', 'KB');
Albeit a bit stale, this library offers a tested and robust conversion API:
https://github.com/gabrielelana/byte-units
Once installed:
\ByteUnits\Binary::bytes(1024)->format();
// Output: "1.00KiB"
And to convert in the other direction:
\ByteUnits\Binary::parse('1KiB')->numberOfBytes();
// Output: "1024"
Beyond basic conversion, it offers methods for addition, subtraction, comparison, etc.
I am no way affiliated with this library.
I did this converting all input to byte and so converting to any output needed. Also, I used a auxiliar function to get base 1000 or 1024, but left it flex to decide use 1024 on popular type (without 'i', like MB instead of MiB).
public function converte_binario($size=0,$format_in='B',$format_out='MB',$force_in_1024=false,$force_out_1024=false,$precisao=5,$return_format=true,$decimal=',',$centena=''){
$out = false;
if( (is_numeric($size)) && ($size>0)){
$in_data = $this->converte_binario_aux($format_in,$force_in_1024);
$out_data = $this->converte_binario_aux($format_out,$force_out_1024);
// se formato de entrada e saída foram encontrados
if( ((isset($in_data['sucesso'])) && ($in_data['sucesso']==true)) && ((isset($out_data['sucesso'])) && ($out_data['sucesso']==true))){
// converte formato de entrada para bytes.
$size_bytes_in = $size * (pow($in_data['base'], $in_data['pot']));
$size_byte_out = (pow($out_data['base'], $out_data['pot']));
// transforma bytes na unidade de destino
$out = number_format($size_bytes_in / $size_byte_out,$precisao,$decimal,$centena);
if($return_format){
$out .= $format_out;
}
}
}
return $out;
}
public function converte_binario_aux($format=false,$force_1024=false){
$out = [];
$out['sucesso'] = false;
$out['base'] = 0;
$out['pot'] = 0;
if((is_string($format) && (strlen($format)>0))){
$format = trim(strtolower($format));
$units_1000 = ['b','kb' ,'mb' ,'gb' ,'tb' ,'pb' ,'eb' ,'zb' ,'yb' ];
$units_1024 = ['b','kib','mib','gib','tib','pib','eib','zib','yib'];
$pot = array_search($format,$units_1000);
if( (is_numeric($pot)) && ($pot>=0)){
$out['pot'] = $pot;
$out['base'] = 1000;
$out['sucesso'] = true;
}
else{
$pot = array_search($format,$units_1024);
if( (is_numeric($pot)) && ($pot>=0)){
$out['pot'] = $pot;
$out['base'] = 1024;
$out['sucesso'] = true;
}
}
if($force_1024){
$out['base'] = 1024;
}
}
return $out;
}
function byte_format($size) {
$bytes = array( ' KB', ' MB', ' GB', ' TB' );
foreach ($bytes as $val) {
if (1024 <= $size) {
$size = $size / 1024;
continue;
}
break;
}
return round( $size, 1 ) . $val;
}
Here is simplified implementation of the Drupal format_size function:
/**
* Generates a string representation for the given byte count.
*
* #param $size
* A size in bytes.
*
* #return
* A string representation of the size.
*/
function format_size($size) {
if ($size < 1024) {
return $size . ' B';
}
else {
$size = $size / 1024;
$units = ['KB', 'MB', 'GB', 'TB'];
foreach ($units as $unit) {
if (round($size, 2) >= 1024) {
$size = $size / 1024;
}
else {
break;
}
}
return round($size, 2) . ' ' . $unit;
}
}
Base on Leo's answer, add
Support for negative
Support 0 < value < 1 ( Ex: 0.2, will cause log(value) = negative number )
If you want max unit to Mega, change to $units = explode(' ', ' K M');
function formatUnit($value, $precision = 2) {
$units = explode(' ', ' K M G T P E Z Y');
if ($value < 0) {
return '-' . formatUnit(abs($value));
}
if ($value < 1) {
return $value . $units[0];
}
$power = min(
floor(log($value, 1024)),
count($units) - 1
);
return round($value / pow(1024, $power), $precision) . $units[$power];
}
It's a little late but a slightly faster version of the accepted answer is below:
function formatBytes($bytes, $precision)
{
$unit_list = array
(
'B',
'KB',
'MB',
'GB',
'TB',
);
$bytes = max($bytes, 0);
$index = floor(log($bytes, 2) / 10);
$index = min($index, count($unit_list) - 1);
$bytes /= pow(1024, $index);
return round($bytes, $precision) . ' ' . $unit_list[$index];
}
It's more efficient, due to performing a single log-2 operation instead of two log-e operations.
It's actually faster to do the more obvious solution below, however:
function formatBytes($bytes, $precision)
{
$unit_list = array
(
'B',
'KB',
'MB',
'GB',
'TB',
);
$index_max = count($unit_list) - 1;
$bytes = max($bytes, 0);
for ($index = 0; $bytes >= 1024 && $index < $index_max; $index++)
{
$bytes /= 1024;
}
return round($bytes, $precision) . ' ' . $unit_list[$index];
}
This is because as the index is calculated at the same time as the value of the number of bytes in the appropriate unit. This cut the execution time by about 35% (a 55% speed increase).
Another condensed implementation which can translate to the base 1024 (binary) or base 1000 (decimal) and also works with incredibly large numbers hence of the use of the bc library:
function renderSize($byte,$precision=2,$mibi=true)
{
$base = (string)($mibi?1024:1000);
$labels = array('K','M','G','T','P','E','Z','Y');
for($i=8;$i>=1;$i--)
if(bccomp($byte,bcpow($base, $i))>=0)
return bcdiv($byte,bcpow($base, $i), $precision).' '.$labels[$i-1].($mibi?'iB':'B');
return $byte.' Byte';
}
I figured I would add a meshing of two submitters code (Using John Himmelman's code, which is in this thread, and using Eugene Kuzmenko's code) that I'm using.
function swissConverter($value, $format = true, $precision = 2) {
//Below converts value into bytes depending on input (specify mb, for
//example)
$bytes = preg_replace_callback('/^\s*(\d+)\s*(?:([kmgt]?)b?)?\s*$/i',
function ($m) {
switch (strtolower($m[2])) {
case 't': $m[1] *= 1024;
case 'g': $m[1] *= 1024;
case 'm': $m[1] *= 1024;
case 'k': $m[1] *= 1024;
}
return $m[1];
}, $value);
if(is_numeric($bytes)) {
if($format === true) {
//Below converts bytes into proper formatting (human readable
//basically)
$base = log($bytes, 1024);
$suffixes = array('', 'KB', 'MB', 'GB', 'TB');
return round(pow(1024, $base - floor($base)), $precision) .' '.
$suffixes[floor($base)];
} else {
return $bytes;
}
} else {
return NULL; //Change to prefered response
}
}
This uses Eugene's code to format the $value into bytes (I keep my data in MB, so it converts my data: 10485760 MB into 10995116277760) - it then uses John's code to convert it into the proper display value (10995116277760 into 10 TB).
I've found this really helpful - so my thanks to the two submitters!
I developed my own function that convert human readable memory size to different sizes.
function convertMemorySize($strval, string $to_unit = 'b')
{
$strval = strtolower(str_replace(' ', '', $strval));
$val = floatval($strval);
$to_unit = strtolower(trim($to_unit))[0];
$from_unit = str_replace($val, '', $strval);
$from_unit = empty($from_unit) ? 'b' : trim($from_unit)[0];
$units = 'kmgtph'; // (k)ilobyte, (m)egabyte, (g)igabyte and so on...
// Convert to bytes
if ($from_unit !== 'b')
$val *= 1024 ** (strpos($units, $from_unit) + 1);
// Convert to unit
if ($to_unit !== 'b')
$val /= 1024 ** (strpos($units, $to_unit) + 1);
return $val;
}
convertMemorySize('1024Kb', 'Mb'); // 1
convertMemorySize('1024', 'k') // 1
convertMemorySize('5.2Mb', 'b') // 5452595.2
convertMemorySize('10 kilobytes', 'bytes') // 10240
convertMemorySize(2048, 'k') // By default convert from bytes, result is 2
This function accepts any memory size abbreviation like "Megabyte, MB, Mb, mb, m, kilobyte, K, KB, b, Terabyte, T...." so it is typo safe.

Categories