Here you can find the source of formatData(double aAmount)
Parameter | Description |
---|---|
aAmount | the amount in bytes |
public static String formatData(double aAmount)
//package com.java2s; /*//from w w w .j ava 2 s. co m * Copyright 2006 The National Library of New Zealand * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.text.DecimalFormat; public class Main { /** * Format the amount of data in bytes to a nice readable string * @param aAmount the amount in bytes * @return a nice data size string */ public static String formatData(double aAmount) { DecimalFormat df = new DecimalFormat("#,###,###.##"); String type = ""; double niceDataDown = 0; double dataDown = aAmount; if (dataDown < 1024) { niceDataDown = dataDown; type = " bytes"; } else if ((dataDown / 1024) < 1024) { niceDataDown = dataDown / 1024; type = " KB"; } else if ((dataDown / (1024 * 1024)) < 1024) { niceDataDown = dataDown / (1024 * 1024); type = " MB"; } else if ((dataDown / (1024 * 1024 * 1024)) < 1024) { niceDataDown = dataDown / (1024 * 1024 * 1024); type = " GB"; } return df.format(niceDataDown) + type; } }