473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Calculate bytes in GB,MB format

190 100+
Hi,

I calculate a folder size.But it is return in bytes format. I want to this in gb or mb size according to the size.

Is there any function.?
Apr 30 '08 #1
2 12410
Could you not simply divide the number of bytes returned by 1048576 to get the megabytes or by 1073741824 to get the gigabytes?

Then after you calculate the Mb and the Gb in the function do somethinig like:
Expand|Select|Wrap|Line Numbers
  1. if($gigabytes > 1){
  2.     return $gigabytes." Gb";
  3. }else if($megabytes > 1){
  4.     return $megabytes." Mb";
  5. }else{
  6.     return $bytes." bytes";
  7. }
  8.  
That will return either Gb, Mb or just bytes depending on the size.

Is this what you meant??
Apr 30 '08 #2
Atli
5,058 Expert 4TB
Hi.

I couldn't find a standard function to do this, but I've used this in some of my projects:
Expand|Select|Wrap|Line Numbers
  1. function bytesConvert($bytes)
  2. {
  3.     $ext = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  4.     $unitCount = 0;
  5.     for(; $bytes > 1024; $unitCount++) $bytes /= 1024;
  6.     return $bytes ." ". $ext[$unitCount];
  7. }
  8.  
Apr 30 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
96
by: david ullua | last post by:
I am reading "Joel on Software" these days, and am in stuck with the question of "how to calculate bitsize of a byte" which is listed as one of the basic interview questions in Joel's book. Anyone...
1
by: matturbanowski | last post by:
I am wondering, does anyone know if there is an easy way in VB (ASP.NET) to calculate what format a date time has been written in? Supposing you entered a value into a text box, it is easy enough...
13
by: Angus | last post by:
Hello I have a stream of bytes - unsigned char*. But the 'string' may contain embedded nulls. So not like a traditional c string terminated with a null. I need to calculate the length of...
49
by: seema | last post by:
Hi all, How to calculate upper 4 bytes and lower 4 bytes of a value? Can somebody please explain?? say for example, unsigned int scnlen_lo; int scnlen_hi; unsigned long long scnlen; ...
5
by: JingL | last post by:
Hi, I am trying to convert 4 bytes to IEEE 32 Bit Floating point format in C# .Net the bytes format is: 41 20 00 00 Can anyone help me?
16
by: Alexio | last post by:
Am new to javascript. I need to format the currency in the as numbers are entered in textboxes. I can format the currency in the calculations and display in the totals but can not figure out how to...
2
by: pumeta2001 | last post by:
Hi everybody, I am programming an application to send data using UDP sockets with Python 3.1. The command socket.send requires data in bytes format. The problem I am having is that the...
2
Mas Juliza Alias
by: Mas Juliza Alias | last post by:
I am developing a program of an information system for reservoir operation. I have a table, MSFlexGrid1 with 4 columns of Initial Depth, End Depth, Cut Volume, and Fill Volume (see attachment...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.