Java Size getSizeText(long size)

Here you can find the source of getSizeText(long size)

Description

get Size Text

License

Open Source License

Declaration

public static String getSizeText(long size) 

Method Source Code


//package com.java2s;
/*/*from  w  ww .j av a  2  s .  c o m*/
 * Copyright 2014 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */

import java.text.DecimalFormat;

public class Main {
    public static String getSizeText(long size) {
        DecimalFormat df = new DecimalFormat("0.00");
        if (size >= 0L && size < 1024) {
            return size + "B";
        } else if (size >= 1024 && size < 1048576) {
            double fileSize = (double) size / 1024;
            return df.format(fileSize) + "KB";
        } else {
            double fileSize = (double) size / 1048576;
            return df.format(fileSize) + "MB";
        }
    }
}

Related

  1. getSizeFormat()
  2. getSizeInKB(String size)
  3. getSizeString(long bytes)
  4. getSizeString(long number, Locale locale)
  5. getSizeString(long size)
  6. getSpaceSizeFromByte(long xB)
  7. getStringSize(String string)
  8. padString(String stringToPad, int size)
  9. read(byte[] input, int size)