Java Size readableSize(long size, DecimalFormat format)

Here you can find the source of readableSize(long size, DecimalFormat format)

Description

readable Size

License

Open Source License

Declaration

public static String readableSize(long size, DecimalFormat format) 

Method Source Code


//package com.java2s;
/*//from w w  w  . j av a2  s . c o m
 * Copyright (c) 2011 - 2012. Elega9t Ltd. All rights reserved.
 * ELEGA9T PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.Copyright (c) 2011 - 2012. Elega9t Ltd. All rights reserved.
 */

import java.text.DecimalFormat;

public class Main {
    private static final DecimalFormat FILE_SIZE_FORMAT = new DecimalFormat("#,##0.#");

    public static String readableSize(long size, DecimalFormat format) {
        if (size <= 0)
            return "0";
        final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
        int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
        return format.format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
    }

    public static String readableSize(long size) {
        return readableSize(size, FILE_SIZE_FORMAT);
    }
}

Related

  1. read(byte[] input, int size)
  2. readableBytes(long byteSize)
  3. readableSize(long num)
  4. readableSize(long size)
  5. readableSize(long size)
  6. renderNumber(int n, int size)
  7. size(long l)
  8. size2human(long size)
  9. sizeRenderer(String value)