Java Memory Format formatMemory(long mem)

Here you can find the source of formatMemory(long mem)

Description

format Memory

License

Apache License

Parameter

Parameter Description
mem a parameter

Return

formatted memory

Declaration

public static String formatMemory(long mem) 

Method Source Code

//package com.java2s;
/**//from   ww w. j  av  a2  s.  c  om
 * OLAT - Online Learning and Training<br>
 * http://www.olat.org
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); <br>
 * you may not use this file except in compliance with the License.<br>
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing,<br>
 * software distributed under the License is distributed on an "AS IS" BASIS, <br>
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
 * See the License for the specific language governing permissions and <br>
 * limitations under the License.
 * <p>
 * Copyright (c) 1999-2006 at Multimedia- & E-Learning Services (MELS),<br>
 * University of Zurich, Switzerland.
 * <p>
 */

public class Main {
    /**
     * @param mem
     * @return formatted memory
     */
    public static String formatMemory(long mem) {
        long kb = mem / 1024;
        long mb = kb / 1024;
        if (mb > 0)
            return mb + " MB";
        else if (kb > 0)
            return kb + " KB";
        else
            return mem + " B";
    }
}

Related

  1. formatMemory(Integer memory)
  2. formatMemorySize(long arg)