Here you can find the source of formatFileSize(long size)
Parameter | Description |
---|---|
intValue | Int value representation |
public static String formatFileSize(long size)
//package com.java2s; /*/*from w ww .j a v a 2 s . c om*/ * @(#)TextUtility.java * * Copyright (c) 2003 DCIVision Ltd * All rights reserved. * * This software is the confidential and proprietary information of DCIVision * Ltd ("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 DCIVision Ltd. */ import java.text.NumberFormat; public class Main { /** * formatFileSize * * Convert a int representation into the label of file size. * * @param intValue Int value representation * @return String The String format after convertion */ public static String formatFileSize(long size) { StringBuffer str = new StringBuffer(); NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(1); nf.setMinimumFractionDigits(1); if (size <= 0) { return "0 bytes"; } if (size < 1024) { str.append(size).append(" bytes"); } else if (size < 1048576) { str.append(nf.format(size / 1024.0)).append(" KB"); } else if (size < 1073741824) { str.append(nf.format((size / 1024.0) / 1024.0)).append(" MB"); } else { str.append(nf.format(size / (1024.0 * 1024 * 1024))).append(" GB"); } return str.toString(); } }