Here you can find the source of toByteUnit(long values)
public static String toByteUnit(long values)
//package com.java2s; /*/* w ww. j a v a2s . c om*/ Copyright 2015 HJOW 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. */ public class Main { public static String toByteUnit(long values) { double calcs = 0.0; if (values == 0) return "0"; if (values == 1) return "1 byte"; if (values < 1024) return String.valueOf(values) + " bytes"; calcs = values / 1024.0; if (calcs < 1024.0) return String.format("%.3f KB", calcs); calcs = calcs / 1024.0; if (calcs < 1024.0) return String.format("%.3f MB", calcs); calcs = calcs / 1024.0; if (calcs < 1024.0) return String.format("%.3f GB", calcs); calcs = calcs / 1024.0; if (calcs < 1024.0) return String.format("%.3f TB", calcs); calcs = calcs / 1024.0; if (calcs < 1024.0) return String.format("%.3f PB", calcs); calcs = calcs / 1024.0; if (calcs < 1024.0) return String.format("%.3f EB", calcs); calcs = calcs / 1024.0; if (calcs < 1024.0) return String.format("%.3f ZB", calcs); calcs = calcs / 1024.0; if (calcs < 1024.0) return String.format("%.3f YB", calcs); return String.format("%.3f YB", calcs); } }