Java Swing Text Format formatAreaMetric(BigDecimal area)

Here you can find the source of formatAreaMetric(BigDecimal area)

Description

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

License

Open Source License

Parameter

Parameter Description
area The area in metres to format

Return

The formated area or null if area is null

Declaration

public static String formatAreaMetric(BigDecimal area) 

Method Source Code


//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;
    }
}

Related

  1. findNext(boolean reverse, int pos)
  2. formartCpf(String cpf)
  3. format(String pattern, Object value)
  4. formata(String s, String mascara)
  5. formatAreaImperial(BigDecimal areaDec)
  6. formatCEP(String str)
  7. formatCpfCnpj(String cpfCnpj)
  8. formatCpfCNPJ(String str)
  9. formatNumericString(String string, String mask)