mikevarela Posted January 8, 2010 Share Posted January 8, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/187737-convert-number-to-decimal-please-help/ Share on other sites More sharing options...
Psycho Posted January 8, 2010 Share Posted January 8, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187737-convert-number-to-decimal-please-help/#findComment-991163 Share on other sites More sharing options...
mikevarela Posted January 8, 2010 Author Share Posted January 8, 2010 Thanks, I'd appreciate the function.... but I also figured it out. If file is larger than 1000 then divide by 1024 for GB amount, if not, then state as MB Quote Link to comment https://forums.phpfreaks.com/topic/187737-convert-number-to-decimal-please-help/#findComment-991290 Share on other sites More sharing options...
Mchl Posted January 8, 2010 Share Posted January 8, 2010 If file is larger than 1000 then divide by 1024 for GB amount, if not, then state as MB Actually, if file is larger than 1024 Quote Link to comment https://forums.phpfreaks.com/topic/187737-convert-number-to-decimal-please-help/#findComment-991316 Share on other sites More sharing options...
Psycho Posted January 8, 2010 Share Posted January 8, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/187737-convert-number-to-decimal-please-help/#findComment-991354 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.