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

BigDecimalconvert(long value, long factor, int comma)
Converter from long to bigDecimal
BigDecimal retVal = new BigDecimal(value);
retVal = retVal.divide(new BigDecimal(factor), comma, BigDecimal.ROUND_HALF_UP);
return retVal;
LongconvertBytesToKBytes(String value)
convert Bytes to KiloBytes
if (null == value) {
    return 0L;
BigDecimal val = new BigDecimal(value);
BigDecimal kbconverter = new BigDecimal(KILOBYTECONVERTERVALUE);
BigDecimal result = val.divide(kbconverter, RoundingMode.CEILING);
if (result.longValue() == 0) {
    return 1L;
...
StringconvertBytesToUnit(long bytes)
Converts bytes to a representable unit, such as MB,GB,TB etc.
String unit = " kB";
BigDecimal divisor = new BigDecimal(1000);
BigDecimal value = new BigDecimal(bytes);
value = value.divide(divisor);
if (value.compareTo(divisor) == 1) {
    value = value.divide(divisor);
    unit = " MB";
if (value.compareTo(divisor) == 1) {
    value = value.divide(divisor);
    unit = " GB";
if (value.compareTo(divisor) == 1) {
    value = value.divide(divisor);
    unit = " TB";
if (value.compareTo(divisor) == 1) {
    value = value.divide(divisor);
    unit = " PB";
return value.setScale(2, RoundingMode.HALF_UP) + unit;
intconvertCount(Object obj)
This method safely converts the result of a SELECT COUNT(*) to an int.
if (obj instanceof Integer) {
    return (Integer) obj;
} else if (obj instanceof Long) {
    return ((Long) obj).intValue();
} else if (obj instanceof BigDecimal) {
    return ((BigDecimal) obj).intValue();
throw new RuntimeException("Cannot convert type " + obj.getClass() + " to an int");
...
longconvertDurationToMillis(String time)
Calls the correct functions to convert the time period to milliseconds
if (time == null || (time != null && "".equals(time))) {
    return 0;
BigDecimal milliseconds = null;
String line = time;
String pattern = "(\\d+.\\d+|\\d+)(\\w+)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
...
StringconvertedFileSize(Long fileSize)
converted File Size
String convertedSize = null;
if (fileSize >= 1024 * 1024) {
    convertedSize = BigDecimal.valueOf(fileSize).divide(BigDecimal.valueOf(1024 * 1024), 1,
            RoundingMode.HALF_UP) + "M";
} else if (fileSize >= 1024) {
    convertedSize = BigDecimal.valueOf(fileSize).divide(BigDecimal.valueOf(1024), 1, RoundingMode.HALF_UP)
            + "K";
} else {
...
IntegerconvertFromMsToMinutes(Integer duration)
convert From Ms To Minutes
return (duration != null) ? duration / MILISECONDS_IN_SECOND / SECONDS_IN_MINUTE
        : BigDecimal.ZERO.intValue();
doubleconvertGeoCoordinateToDouble(int point)
Takes a Lat or Long as an int and converts to a double.
double convertedPoint = ((double) point) / LAT_LONG_CONVERSION_FACTOR;
BigDecimal bd = new BigDecimal(convertedPoint);
bd.setScale(LAT_LONG_DECIMAL_PRECISION, BigDecimal.ROUND_HALF_UP);
return bd.doubleValue();
BigDecimalconvertImperialToMetric(String areaImp)
Converts an area in imperial format to its metric equivalent.
BigDecimal result = null;
if (areaImp != null && !areaImp.trim().isEmpty()) {
    String regex = "(.*?)a| (.*?)r|(.*?)r|a(.*?)r| (.*?)p|(.*?)p|r(.*?)p|a(.*?)p";
    Pattern pattern = Pattern.compile(regex);
    if (pattern.matcher(areaImp.toLowerCase()).matches()) {
        Double acre = new Double(0);
        Double rood = new Double(0);
        Double perch = new Double(0);
...
BigDecimalconvertJcoDecimalDefaultValue(String jcoDefaultDecimalValue)
Converts the JCo string representation for the default value of a decimal type list parameter field into a BigDecimal.
if (jcoDefaultDecimalValue == null) {
    return null;
try {
    return new BigDecimal(jcoDefaultDecimalValue);
} catch (NumberFormatException e) {
    return null;