Here you can find the source of formatStore(long store)
public static String formatStore(long store)
//package com.java2s; /*// w w w. j a v a2 s . c om * Copyright 1999-2012 Alibaba Group. * * 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.math.BigDecimal; public class Main { private static final long KB = 1024L; private static final long MB = 1024L * 1024; private static final long GB = 1024L * 1024 * 1024; private static final long TB = 1024L * 1024 * 1024 * 1024; public static String formatStore(long store) { if (store > TB) { double mem = divider(store, TB, 2); return new StringBuilder(String.valueOf(mem)).append(" ").append("TB").toString(); } else if (store > GB) { double mem = divider(store, GB, 2); return new StringBuilder(String.valueOf(mem)).append(" ").append("GB").toString(); } else if (store > MB) { double mem = divider(store, MB, 2); return new StringBuilder(String.valueOf(mem)).append(" ").append("MB").toString(); } else if (store > KB) { double mem = divider(store, KB, 2); return new StringBuilder(String.valueOf(mem)).append(" ").append("KB").toString(); } else { return new StringBuilder(String.valueOf(store)).append(" ").append("B").toString(); } } public static double divider(double d1, double d2, int scale) { if (d2 == 0) { return 0; } BigDecimal bd1 = new BigDecimal(Double.toString(d1)); BigDecimal bd2 = new BigDecimal(Double.toString(d2)); return bd1.divide(bd2, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); } }