Java JTextField Number showInDecimalFormat(JTextField pJTextField)

Here you can find the source of showInDecimalFormat(JTextField pJTextField)

Description

show In Decimal Format

License

Open Source License

Declaration

public static void showInDecimalFormat(JTextField pJTextField) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import javax.swing.*;

import java.text.ParseException;

public class Main {
    public static void showInDecimalFormat(JTextField pJTextField) {
        pJTextField.setText(formatNumber(getDecimalNumber(pJTextField
                .getText().trim())));/*from w w w  . j  av  a 2 s .c o m*/
    }

    public static String formatNumber(double pValue) {
        java.text.DecimalFormat formatDecimal = new java.text.DecimalFormat(
                ",##0.00");
        java.text.DecimalFormatSymbols symbols = new java.text.DecimalFormatSymbols();
        symbols.setGroupingSeparator(',');
        symbols.setDecimalSeparator('.');
        formatDecimal.setDecimalFormatSymbols(symbols);
        return formatDecimal.format(pValue).toString();
    }

    public static String formatNumber(double pValue, int pDecimales) {
        java.text.DecimalFormat formatDecimal = new java.text.DecimalFormat(
                ",##0." + caracterIzquierda("", pDecimales, "0"));
        java.text.DecimalFormatSymbols symbols = new java.text.DecimalFormatSymbols();
        symbols.setGroupingSeparator(',');
        symbols.setDecimalSeparator('.');
        formatDecimal.setDecimalFormatSymbols(symbols);
        return formatDecimal.format(pValue).toString();
    }

    public static String formatNumber(double pValue, String pFormatNumber) {
        java.text.DecimalFormat formatDecimal = new java.text.DecimalFormat(
                pFormatNumber);
        java.text.DecimalFormatSymbols symbols = new java.text.DecimalFormatSymbols();
        symbols.setGroupingSeparator(',');
        symbols.setDecimalSeparator('.');
        formatDecimal.setDecimalFormatSymbols(symbols);
        return formatDecimal.format(pValue).toString();
    }

    public static double getDecimalNumber(String pDecimal) {
        double decimalNumber = 0.00;
        try {
            java.text.DecimalFormat formatDecimal = new java.text.DecimalFormat(
                    "###,###,##0.00");
            java.text.DecimalFormatSymbols symbols = new java.text.DecimalFormatSymbols();
            symbols.setGroupingSeparator(',');
            symbols.setDecimalSeparator('.');
            formatDecimal.setDecimalFormatSymbols(symbols);
            decimalNumber = formatDecimal.parse(pDecimal).doubleValue();
        } catch (ParseException errParse) {
            errParse.printStackTrace();
        }
        return decimalNumber;
    }

    public static double getDecimalNumber(String pDecimal, int pDecimales) {
        double decimalNumber = 0.00;
        try {
            java.text.DecimalFormat formatDecimal = new java.text.DecimalFormat(
                    "###,##0." + caracterIzquierda("", pDecimales, "0"));
            java.text.DecimalFormatSymbols symbols = new java.text.DecimalFormatSymbols();

            symbols.setGroupingSeparator(',');
            symbols.setDecimalSeparator('.');
            formatDecimal.setDecimalFormatSymbols(symbols);
            decimalNumber = formatDecimal.parse(pDecimal).doubleValue();
        } catch (ParseException errParse) {
            errParse.printStackTrace();
        }
        return decimalNumber;
    }

    public static String caracterIzquierda(int parmint, int parmLen,
            String parmCaracter) {
        return caracterIzquierda(String.valueOf(parmint), parmLen,
                parmCaracter);
    }

    public static String caracterIzquierda(long parmint, int parmLen,
            String parmCaracter) {
        return caracterIzquierda(String.valueOf(parmint), parmLen,
                parmCaracter);
    }

    public static String caracterIzquierda(double parmint, int parmLen,
            String parmCaracter) {
        return caracterIzquierda(String.valueOf(parmint), parmLen,
                parmCaracter);
    }

    public static String caracterIzquierda(String parmString, int parmLen,
            String parmCaracter) {

        String tempString = parmString;

        if (tempString.length() > parmLen)
            tempString = tempString.substring(
                    tempString.length() - parmLen, tempString.length());
        else {
            while (tempString.length() < parmLen)
                tempString = parmCaracter + tempString;
        }

        return tempString;

    }
}

Related

  1. parseIntFromField(JTextField jField, int pDefault)
  2. placeDoubleTextInField(JTextField inField, int length, double toPlace)
  3. readDoubleFromField(JTextField inField, double defaultValue)
  4. setNumericAndCharOnly(final javax.swing.JTextField textField)
  5. setTextField(javax.swing.JTextField textField, double value, int precision)
  6. validateDoubleInput(Component parentComponent, JLabel label, JTextField textField, String valueDescription, String errorTitle, boolean positiveValue, boolean showMessage, boolean valid)
  7. verifyNumber_geq(JTextField field, Long number)
  8. within(javax.swing.JTextField input, double low, double high, Vector errMsgList, String errMsg)