Here you can find the source of formatBytes(final double bytes)
Parameter | Description |
---|---|
bytes | The number of bytes to format |
public static String formatBytes(final double bytes)
//package com.java2s; /*//from w ww .j av a 2 s .c o m * Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). * All rights reserved. * This component and the accompanying materials are made available * under the terms of "Eclipse Public License v1.0" * which accompanies this distribution, and is available * at the URL "http://www.eclipse.org/legal/epl-v10.html". * * Initial Contributors: * Nokia Corporation - initial contribution. * * Contributors: * * Description: Definitions for the class GraphUtils * */ import java.text.DecimalFormat; public class Main { private static final DecimalFormat MB_FORMAT = new DecimalFormat("#####.0"); private static final DecimalFormat BYTES_FORMAT = new DecimalFormat("#####.##"); /** * Formats the given number of bytes into an easily readable format * * @param bytes * The number of bytes to format * @return String containing a formatted version of the input data */ public static String formatBytes(final double bytes) { String scaledY; if (bytes < 10000) { scaledY = BYTES_FORMAT.format((long) bytes) + " B"; } else if (bytes <= 500 * 1024) { scaledY = BYTES_FORMAT.format(bytes / 1024) + " KB"; } else { scaledY = MB_FORMAT.format(((float) bytes / (1024 * 1024))) + " MB"; } return scaledY; } }