Java Number Convert convertImperialToMetric(String areaImp)

Here you can find the source of convertImperialToMetric(String areaImp)

Description

Converts an area in imperial format to its metric equivalent.

License

Open Source License

Parameter

Parameter Description
areaImp The area in imperial format. Acre must have a suffix of a, roods must have a suffix of r and perches must have a suffix of p. E.g. 5a 3r 30.5p

Return

The metric value for the imperial area or null if the imperial value area could not be parsed.

Declaration

public static BigDecimal convertImperialToMetric(String areaImp) 

Method Source Code


//package com.java2s;
import java.math.BigDecimal;
import java.math.RoundingMode;

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

public class Main {
    public static Double METRES_IN_ACRE = new Double(4046.8564224);

    /**/*  w w w .  j  av a 2 s . co  m*/
     * Converts an area in imperial format to its metric equivalent. If the
     * imperial value fails to match the expected pattern, a NULL value is
     * returned.
     *
     * @param areaImp The area in imperial format. Acre must have a suffix of a,
     * roods must have a suffix of r and perches must have a suffix of p. E.g.
     * 5a 3r 30.5p
     * @return The metric value for the imperial area or null if the imperial
     * value area could not be parsed.
     */
    public static BigDecimal convertImperialToMetric(String areaImp) {
        BigDecimal result = null;
        if (areaImp != null && !areaImp.trim().isEmpty()) {
            // Use a pattern matcher to parse out the imperial area values. Look 
            // for a (acre), r (rood) and p (perch) and various combinations of
            // those abbreviations, with and without spaces. 
            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);
                Matcher matcher = pattern.matcher(areaImp.toLowerCase());
                while (matcher.find()) {
                    String tmp = matcher.group();
                    if (tmp != null) {
                        if (tmp.endsWith("a")) {
                            // Get the acre value removing any reference to a or ,
                            acre = new Double(tmp.replaceAll("a|,", "").trim());
                        }
                        if (tmp.endsWith("r")) {
                            // Get the rood value removing any refernece to a or r
                            rood = new Double(tmp.replaceAll("r|a", "").trim());
                        }
                        if (tmp.endsWith("p")) {
                            // Get the perch value removing any reference ot p, a or r
                            perch = new Double(tmp.replaceAll("p|a|r", "").trim());
                        }
                    }
                }
                // Calculate the area meters for the imperial value. 
                Double areaTmp = (acre + (rood / 4) + (perch / 160)) * METRES_IN_ACRE;
                result = new BigDecimal(areaTmp).setScale(1, RoundingMode.DOWN);
            }
        }
        return result;
    }
}

Related

  1. convertCount(Object obj)
  2. convertDurationToMillis(String time)
  3. convertedFileSize(Long fileSize)
  4. convertFromMsToMinutes(Integer duration)
  5. convertGeoCoordinateToDouble(int point)
  6. convertJcoDecimalDefaultValue( String jcoDefaultDecimalValue)
  7. convertMB2GB(int valueMB)
  8. convertMinutesToHours(Long minutes)
  9. convertNanoDiff(long startNanos, long stopNanos, TimeUnit timeUnit)