Java Utililty Methods Number Convert

List of utility methods to do Number Convert

Description

The list of methods to do Number Convert are organized into topic(s).

Method

doubleconvertMB2GB(int valueMB)
Convert a decimal MB(megabyte) to decimal GB(gigabyte)

Officially, there are 1000 (10^3) megabytes (MB) in a gigabyte (GB)
A Memory manufacturer definition is 1024 (2^10) megabytes per gigabyte, but this is more properly called a gigabinary byte (GiB), sometimes contracted to gibibyte.

return (new BigDecimal(valueMB).divide(DECIMAL_KILO).doubleValue());
BigDecimalconvertMinutesToHours(Long minutes)
convert Minutes To Hours
if (minutes == null) {
    return BigDecimal.ZERO;
} else {
    return new BigDecimal(minutes.doubleValue() / 60).setScale(1, BigDecimal.ROUND_HALF_UP);
BigDecimalconvertNanoDiff(long startNanos, long stopNanos, TimeUnit timeUnit)
convert Nano Diff
BigDecimal diffNanos = new BigDecimal(stopNanos - startNanos);
return diffNanos.divide(BigDecimal.valueOf(timeUnit.toNanos(1)));
BigDecimalconvertNullToOne(final Object value)
convert Null To One
if (value == null) {
    return BigDecimal.ONE;
if (value instanceof BigDecimal) {
    return (BigDecimal) value;
return BigDecimal.valueOf(Double.valueOf(value.toString()));
StringconvertScientificToStandard(double value)
convert Scientific To Standard
return (new BigDecimal(Double.toString(value))).toPlainString();
StringconvertSize(Long size)
convert Size
String retStr = "";
for (int i = 3; i > 0; i--) {
    double compSize = Math.pow(1024, i);
    double remainder = size / compSize;
    if (remainder > 1) {
        if (i == 3) {
            retStr = round(remainder, 2, BigDecimal.ROUND_HALF_EVEN) + "G";
            break;
...
StringconvertSizeG1DetailsToSizeG1(final String size, final char units)
Convert SIZE_G1_DETAILS to SIZE_G1.
BigDecimal sizeG1 = new BigDecimal(size);
char unitsG1;
switch (units) {
case 'B':
    sizeG1 = sizeG1.divide(new BigDecimal("1024"));
    unitsG1 = 'K';
    break;
case 'K':
...
StringconvertSizeToMB(long sizeInBytes)
convert Size To MB
double size = sizeInBytes / 1024;
String m = " Kb";
if (size > 1024) {
    size = size / 1024;
    m = " MB";
BigDecimal bd = new BigDecimal(size);
bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
...
longconvertsToLong(double v)
converts To Long
BigDecimal b = new BigDecimal(v);
return b.longValue();
ObjectconvertStringToObjectWrapper(String className, String value)
Converts the string value to the specified wrapper class type
Object o = null;
if (className.equals("java.lang.Integer") || className.equals("int")) {
    o = new Integer(value);
} else if (className.equals("java.lang.Long") || className.equals("long")) {
    o = Long.valueOf(value);
} else if (className.equals("java.lang.Short") || className.equals("short")) {
    o = Short.valueOf(value);
} else if (className.equals("java.lang.Byte") || className.equals("byte")) {
...