Jump to content

Recommended Posts

Hi,

 

I have a number.. let's say it's 2996.

This number needs to be represented on screen as 2.996.

 

I'm creating a calculator for disk size of audio files.. the figure above is MB but I'm running it through a If ... Else statement to change anything over a thousand to GB and I need to insert the decimal for workers to understand the figure.

 

Can't understand how to do it

Link to comment
https://forums.phpfreaks.com/topic/187737-convert-number-to-decimal-please-help/
Share on other sites

2996 Megabytes does not equal 2.996 Gigabytes. You need to convert using base 2.

 

2996 MB ~ 2.926 GB

 

I have a function to do the correct conversions, but it relies on the input being in Bytes. Give me a moment to modify for you.

Here's is a function that will take an amount in any unit and conver to any other unit OR do not specify the output unit and it uses logic to convert it to the most "appropriate" unit.

 

//This function converts untis from one measurement to another. If no output
//measurement is specified, the greatest measurement with a whole number is used
//PARAMETERS:
//
// - $outputUnits [Optional]: Will convert the value to the specified unit.
//        If left blank will convert to the largest possible whole unit.
function convert_units($input, $outputUnits='', $precision=2)
{
    $units = array('B', 'KB', 'MB', 'GB', 'TB');
$input = strtoupper($input);
    $outputUnits = strtoupper($outputUnits);
    //Determine amount, units and exponent of input (bytes is assumed)
    $inputParams = preg_split("/([\d.]+)[ ]*(\w*)/", $input, 0, PREG_SPLIT_DELIM_CAPTURE);
    $inputAmt   = $inputParams[1];
    $inputUnits = (in_array($inputParams[2], $units))? $inputParams[2] : $units[0];
    $inputExp   = array_search($inputUnits, $units);
    $bytes = ($inputAmt * pow(1024, $inputExp));
    //Determine the exponent and units for the output
    $outputExp = (in_array($outputUnits, $units))? array_search($outputUnits, $units) : floor(log($bytes)/log(1024));
    $outputUnits = $units[$outputExp];
    //Detemine the converted value
    $outputAmt   = number_format(($bytes/pow(1024, $outputExp)), $precision);
return "$outputAmt $outputUnits";
}

//Usage

//Convert MB to KB
echo convert_units("2.5MB", "KB"); //Output: 2,560.00 KB
//Convert MB to GB
echo convert_units("2560 MB", "GB"); //Output: 2.50 GB
//Convert GB to KB
echo convert_units("1.23GB", "KB"); //Output: 1,289,748.48 KB


//Convert units to most appropriate value
echo convert_units("2356"); //Output: 2.30 KB
echo convert_units("3845475"); //Output: 3.67 MB
echo convert_units("65987929 KB"); //Output: 62.93 GB
echo convert_units("98566483157"); //Output: 1.80 GB

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.