Java Size Format formatDataSize(long bytes)

Here you can find the source of formatDataSize(long bytes)

Description

format Data Size

License

Open Source License

Declaration

public static String formatDataSize(long bytes) 

Method Source Code

//package com.java2s;
/*/*from   ww w .  j a  va2s  . c o  m*/
 * Copyright (C) 2007, 2008 Quadduc <quadduc@gmail.com>
 * Copyright (C) 2007, 2011 IsmAvatar <IsmAvatar@gmail.com>
 * Copyright (C) 2007 Clam <clamisgood@gmail.com>
 * Copyright (C) 2013, 2014 Robert B. Colton
 *
 * This file is part of LateralGM.
 * LateralGM is free software and comes with ABSOLUTELY NO WARRANTY.
 * See LICENSE for details.
 */

import java.text.DecimalFormat;

public class Main {
    public static String formatDataSize(long bytes) {
        if (bytes <= 0)
            return "0 B";
        final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
        int digits = (int) (Math.log(bytes) / Math.log(1024));
        return new DecimalFormat("#,##0.##").format(bytes / Math.pow(1024, digits)) + " " + units[digits];
    }
}

Related

  1. formatBinarySize(final long l)
  2. formatBytes(double size)
  3. formatBytes(long size)
  4. formatByteSize(long bytes)
  5. formatDataSize(final double dataSize)
  6. formatDecimal(double size)
  7. formatDiskSize(final long diskSize)
  8. formatFileSize(double fileSize, int precision, int unit, boolean showUnit)
  9. formatFileSize(File file)