Here you can find the source of formatAreaMetric(BigDecimal area)
Parameter | Description |
---|---|
area | The area in metres to format |
public static String formatAreaMetric(BigDecimal area)
//package com.java2s; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.DecimalFormat; import java.text.ParseException; import javax.swing.text.NumberFormatter; public class Main { /**/* ww w . j ava 2 s .c o m*/ * Formats a BigDecimal value representing an area in square metres into a * string indicating the area as metres squared (where area is less that * 10,000) or hectares (where area is >= 10,000 * * @param area The area in metres to format * @return The formated area or null if area is null */ public static String formatAreaMetric(BigDecimal area) { String result = null; if (area != null && BigDecimal.ZERO.compareTo(area) < 0) { NumberFormatter areaFormatter = new NumberFormatter(DecimalFormat.getNumberInstance()); try { if (area.compareTo(new BigDecimal("10000")) >= 0) { // The area is >= 10,000 so format as hectares area = area.divide(new BigDecimal("10000")); area = area.setScale(3, RoundingMode.DOWN); result = areaFormatter.valueToString(area) + "ha"; } else { // Format the area has metres squared area = area.setScale(0, RoundingMode.DOWN); result = areaFormatter.valueToString(area) + "m" + (char) 178; // Superscript 2 } } catch (ParseException psex) { } } return result; } }