Here you can find the source of formatAreaImperial(BigDecimal areaDec)
Parameter | Description |
---|---|
area | The area in meters to format |
public static String formatAreaImperial(BigDecimal areaDec)
//package com.java2s; import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.ParseException; import javax.swing.text.NumberFormatter; public class Main { public static Double METRES_IN_ACRE = new Double(4046.8564224); /**/* w w w . j a v a2 s. c om*/ * Formats a BigDecimal value representing an area in square meters an * imperial area (i.e. acres, roods and perches. * * @param area The area in meters to format * @return The formated area or null if area is null */ public static String formatAreaImperial(BigDecimal areaDec) { String result = null; if (areaDec != null && BigDecimal.ZERO.compareTo(areaDec) < 0) { // Use a Double because the BigDecimal division is unreliable result = ""; Double area = areaDec.doubleValue(); NumberFormatter areaFormatter = new NumberFormatter(DecimalFormat.getNumberInstance()); try { // 1a = 4046.8564 square metres Double acresTmp = new Double(area / METRES_IN_ACRE); int acres = acresTmp.intValue(); Double remainder = acresTmp - acres; // 4 roods to an acre Double roodsTmp = new Double(remainder * 4); int roods = roodsTmp.intValue(); remainder = roodsTmp - roods; // 40 perches to a rood Double perches = remainder * 40; // This function introduces some rounding, so increment the // roods if perches is near 40 and adjust the acres accordingly. if (perches >= new Double(39.95)) { roods++; perches = new Double(0); if (roods == 4) { acres++; roods = 0; } } if (acres >= 1) { result = areaFormatter.valueToString(acres) + "a"; } if (roods >= 1) { DecimalFormat f = new DecimalFormat("0r"); result = result + " " + f.format(roods); } if (perches >= new Double(0.05)) { DecimalFormat p = new DecimalFormat("0.0p"); result = result + " " + p.format(perches); } result = result.trim(); } catch (ParseException psex) { } } return result; } }