Java Size Format formatGameSize(int size)

Here you can find the source of formatGameSize(int size)

Description

format Game Size

License

Open Source License

Declaration

public static String formatGameSize(int size) 

Method Source Code

//package com.java2s;

import java.text.DecimalFormat;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static String formatGameSize(int size) {
        DecimalFormat df = new DecimalFormat("0.00");
        if (size > 1024 * 1024) {
            return String.valueOf(df.format((float) size / (1024 * 1024))) + "M";
        } else {/*from w  w  w  .  jav  a  2 s  . c  om*/
            return String.valueOf(df.format((float) size / 1024)) + "K";
        }

    }

    public static String format(String str, Object... args) {
        if (isEmptyOrNull(str))
            return "";
        if (args.length == 0)
            return str;
        String result = str;
        Pattern p = java.util.regex.Pattern.compile("\\{(\\d+)\\}");
        Matcher m = p.matcher(str);
        while (m.find()) {
            int index = Integer.parseInt(m.group(1));
            if (index < args.length) {
                result = result.replace(m.group(), args[index].toString());
            }
        }
        return result;
    }

    public static boolean isEmptyOrNull(String str) {
        return str == null || str.length() == 0 || str.contentEquals("null") || str.trim().equals("");
    }
}

Related

  1. formatFileSize(long size)
  2. formatFileSize(long size)
  3. formatFileSize(long size)
  4. formatFileSize(Long sizeBytes)
  5. formatFilesizeGB(long filesize, int fractionDigits)
  6. formatMemorySize(long size)
  7. formatPartSize(int size, NumberFormat format)
  8. formatSize(double fileSize)
  9. formatSize(double size)