Java Size getSizeInKB(String size)

Here you can find the source of getSizeInKB(String size)

Description

get Size In KB

License

Open Source License

Declaration

public static String getSizeInKB(String size) throws NumberFormatException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.DecimalFormat;

public class Main {
    public static DecimalFormat fFileSize = new DecimalFormat("#.000");

    public static String getSizeInKB(String size) throws NumberFormatException {
        if (size.indexOf("-") != -1 || size.equals("0"))
            return "0";
        String s = size.substring(0, size.length() - 1);
        double ds = 0;
        try {// w w w  .jav  a2s  . c o m
            ds = Double.valueOf(s);
        } catch (NumberFormatException e) {
            System.out.println("NFE getSizeInKB str: " + size);
            throw e;
        }

        String u = size.substring(size.length() - 1);
        int sw = -1;
        if (u.equalsIgnoreCase("B"))
            sw = 0;
        if (u.equalsIgnoreCase("M"))
            sw = 1;
        else if (u.equalsIgnoreCase("G"))
            sw = 2;

        switch (sw) {
        case 0:
            ds = ds / 1000;
            break;
        case 1:
            ds = ds * 1000;
            break;
        case 2:
            ds = ds * 1000000;
            break;
        default:
            break;
        }
        return fFileSize.format(ds);

    }
}

Related

  1. getReadableByteSize(long size)
  2. getReadableSize(long size)
  3. getSize(long octets)
  4. getSizeAll(final long bytes)
  5. getSizeFormat()
  6. getSizeString(long bytes)
  7. getSizeString(long number, Locale locale)
  8. getSizeString(long size)
  9. getSizeText(long size)