Convert KB to MB, GB, TB dynamically

public String size(int size){ String hrSize = ""; int k = size; double m = size/1024; double g = size/1048576; double t = size/1073741824; DecimalFormat dec = new DecimalFormat("0.00"); if (k>0) { hrSize = dec.format(k).concat("KB"); } if (m>0) { hrSize = dec.format(m).concat("MB"); } if (g>0) { hrSize = dec.format(g).concat("GB"); } if (t>0) { hrSize = dec.format(t).concat("TB"); } return hrSize; } 

This is a method that should return a size in GB, MB, KB, or TB. The input value is in KB. for example, the result for 1245 should be 1.21 MB, but I get 1.00 MB.

+13
source share
11 answers

You are doing an integer division . Thus, the result of division is also an integer . The fractional part is truncated.

 so, 1245 / 1024 = 1 

Change the division to floating point division : -

 double m = size/1024.0; double g = size/1048576.0; double t = size/1073741824.0; 

In addition, your comparison is erroneous. You should do a comparison with 1 .

 if (m > 1), if (t > 1), if (g > 1) 

Ideally, I would change your comparison: -

  if (t > 1) { hrSize = dec.format(t).concat("TB"); } else if (g > 1) { hrSize = dec.format(g).concat("GB"); } else if (m > 1) { hrSize = dec.format(m).concat("MB"); } else { hrSize = dec.format(size).concat("KB"); } 

First you need to compare with a higher module, and then go to the bottom.

+27
source

The modified version. Selects only once. Includes Bytes.

 public static String formatFileSize(long size) { String hrSize = null; double b = size; double k = size/1024.0; double m = ((size/1024.0)/1024.0); double g = (((size/1024.0)/1024.0)/1024.0); double t = ((((size/1024.0)/1024.0)/1024.0)/1024.0); DecimalFormat dec = new DecimalFormat("0.00"); if ( t>1 ) { hrSize = dec.format(t).concat(" TB"); } else if ( g>1 ) { hrSize = dec.format(g).concat(" GB"); } else if ( m>1 ) { hrSize = dec.format(m).concat(" MB"); } else if ( k>1 ) { hrSize = dec.format(k).concat(" KB"); } else { hrSize = dec.format(b).concat(" Bytes"); } return hrSize; } 
+36
source

I like it:

 public static String getDynamicSpace(long diskSpaceUsed) { if (diskSpaceUsed <= 0) { return "0"; } final String[] units = new String[] { "B", "KiB", "MiB", "GiB", "TiB" }; int digitGroups = (int) (Math.log10(diskSpaceUsed) / Math.log10(1024)); return new DecimalFormat("#,##0.#").format(diskSpaceUsed / Math.pow(1024, digitGroups)) + " " + units[digitGroups]; } 
+11
source

The problem is that you are using integer division. Change your code to:

 double m = size/1024.0; double g = size/1048576.0; double t = size/1073741824.0; 

In the source code, double m = size/1024 will divide the integer size by 1024 , truncate the result by an integer, and only then convert it to double . That is why the fractional part was lost.

+5
source

Integer division is performed,

ie, 31/15 will result in 2, not 2. whatever

just add the number with D or D , which denotes it as double, and you'll be fine

 double m = size/1024D; double g = size/1048576D; double t = size/1073741824D; 
+3
source

It’s not easy to understand. Rohit Jain mentioned an integer operation. Rounding can also be a problem, since rounding can always be undesirable. I would advise finding an affordable solution, for example, in the triava library.

It can format numbers with arbitrary precision in three different systems (SI, IEC, JEDEC) and various output options. Here are some sample code from triava test blocks :

 UnitFormatter.formatAsUnit(1126, UnitSystem.SI, "B"); // = "1.13kB" UnitFormatter.formatAsUnit(2094, UnitSystem.IEC, "B"); // = "2.04KiB" 

Print exact kilograms, mega values ​​(here with W = Watt):

 UnitFormatter.formatAsUnits(12_000_678, UnitSystem.SI, "W", ", "); // = "12MW, 678W" 

You can pass DecimalFormat to customize the output:

 UnitFormatter.formatAsUnit(2085, UnitSystem.IEC, "B", new DecimalFormat("0.0000")); // = "2.0361KiB" 

For arbitrary operations with kilo or mega values, you can break them into components:

 UnitComponent uc = new UnitComponent(123_345_567_789L, UnitSystem.SI); int kilos = uc.kilo(); // 567 int gigas = uc.giga(); // 123 
+3
source
 String[] fileSizeUnits = {"bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}; public String calculateProperFileSize(double bytes){ String sizeToReturn = "";// = FileUtils.byteCountToDisplaySize(bytes), unit = ""; int index = 0; for(index = 0; index < fileSizeUnits.length; index++){ if(bytes < 1024){ break; } bytes = bytes / 1024; } System.out.println("Systematic file size: " + bytes + " " + fileSizeUnits[index]); sizeToReturn = String.valueOf(bytes) + " " + fileSizeUnits[index]; return sizeToReturn; } 

Just add more file blocks (if they are missing) and you will see the block size to this device (if your file is so long)

+2
source
 class ConverterUtils{ public static void main(String[] args) { System.out.println(getSize(1234567)); } public static String getSize(long size) { String s = ""; double kb = size / 1024; double mb = kb / 1024; double gb = mb / 1024; double tb = gb / 1024; if(size < 1024L) { s = size + " Bytes"; } else if(size >= 1024 && size < (1024L * 1024)) { s = String.format("%.2f", kb) + " KB"; } else if(size >= (1024L * 1024) && size < (1024L * 1024 * 1024)) { s = String.format("%.2f", mb) + " MB"; } else if(size >= (1024L * 1024 * 1024) && size < (1024L * 1024 * 1024 * 1024)) { s = String.format("%.2f", gb) + " GB"; } else if(size >= (1024L * 1024 * 1024 * 1024)) { s = String.format("%.2f", tb) + " TB"; } return s; } } 

To understand better - https://www.techspot.com/news/68482-quickly-convert-between-storage-size-units-kb-mb.html

+1
source

My basic version (you can define some constants instead of calculating POW all the time):

 public static String GetFolderSizeHuman(long aBytes) { if (aBytes < 1024 * 1024) return aBytes + " KB"; else if (aBytes < Math.pow(2, 20) * 1024) return (int) aBytes / Math.pow(2, 20) + " MB"; else if (aBytes < Math.pow(2, 30) * 1024 ) return kGbTbFormatter.format(aBytes / Math.pow(2, 30)) + " GB"; else if (aBytes < Math.pow(2, 40) * 1024) return kGbTbFormatter.format(aBytes / Math.pow(2, 40)) + " TB"; else return "N/A (1TB?)"; } 
0
source

bickster's answer works quite fine, but the problem is that it returns results like 45.00 Bytes and 12.00 KB . In my opinion, the last decimal digits should be removed if they are zeros. Therefore, instead of 45.00 Bytes and 12.00 KB you get 45 B and 12 KB (note that Bytes was changed to B This is only true for uniformity, since we have KB, MB, etc., and not Kilobytes, Megabytes and etc.).

 private boolean isDouble(double value) { if (value % 1 == 0) { Log.d(TAG, "value is " + value + " and is not double"); return false; } else { Log.d(TAG, "value is " + value + " and is double"); return true; } } 

The above method just checks to see if zeros are in the form of decimal digits.

 private String formatFileSize(long size) { String hrSize = null; double b = size; double k = size/1024.0; double m = ((size/1024.0)/1024.0); double g = (((size/1024.0)/1024.0)/1024.0); double t = ((((size/1024.0)/1024.0)/1024.0)/1024.0); DecimalFormat dec1 = new DecimalFormat("0.00"); DecimalFormat dec2 = new DecimalFormat("0"); if (t>1) { hrSize = isDouble(t) ? dec1.format(t).concat(" TB") : dec2.format(t).concat(" TB"); } else if (g>1) { hrSize = isDouble(g) ? dec1.format(g).concat(" GB") : dec2.format(g).concat(" GB"); } else if (m>1) { hrSize = isDouble(m) ? dec1.format(m).concat(" MB") : dec2.format(m).concat(" MB"); } else if (k>1) { hrSize = isDouble(k) ? dec1.format(k).concat(" KB") : dec2.format(k).concat(" KB"); } else { hrSize = isDouble(b) ? dec1.format(b).concat(" B") : dec2.format(b).concat(" B"); } return hrSize; } 
0
source

This is related to the correlation between electric and temporary alternating current. -------------------------- Constant direct current = alternating current direct current = dead current, also known as positive [CN10] negative = DC, so it also refers to religion, so the example that follows a live current is after the death of Christ or before the God of Christ said, let there be a light type of shit, strange madness, hold me and solve my equation to DC dead current / = death of Christ, so that before b there is your timeline, so before Christmas and Christ or so, so use a lot of reasons and variables, for example y + y = y aka y = Tony + y = Sally, so if Tony had apples and Sally took them then (y), otherwise aka, equal, maybe Sally didn't have to fuck, took Tony guys and Maybee. God had to tell Adam and Eve that the apples have not yet ripened, that everything is for today, but here is the key to time travel, but remember that there is no such thing as time for Tony, otherwise Anthony Lowell Willis, please feel free to contact with me by email or by phone. I'm a moron, not a mathematician, haha, please use your own opinion on this topic thanks

0
source

All Articles