Java Number Format Pattern formatIntoCurr(String str_number, int digits)

Here you can find the source of formatIntoCurr(String str_number, int digits)

Description

Use this method like this:
String formatted = Util.formatIntoCurr("123456789.123", 2);
formatted ==> "123,456,789.12"

License

Apache License

Parameter

Parameter Description

Return

formatted String

Declaration

public static String formatIntoCurr(String str_number, int digits) 

Method Source Code

//package com.java2s;
/**/*  w w  w . j  ava 2  s  . c o m*/
 * Copyright 2014 tgrape Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.text.DecimalFormat;

public class Main {
    /**
    * Use this method like this:<br>
    * <font color="#0000ff">String formatted = Util.formatIntoCurr("123456789.123", 2);<br>
    * formatted ==> "123,456,789.12"
    * </font>
    * @param str_numer<br>
    * @param digits<br>
    * @return formatted String
    */
    public static String formatIntoCurr(String str_number, int digits) {
        String pattern = "###,###";
        double value = -1.;
        if (digits > 0)
            pattern += ".";
        for (int i = 0; i < digits; i++) {
            pattern += "#";
        }
        try {
            value = Double.parseDouble(str_number);
        } catch (NumberFormatException e) {
            System.err.println("[Util] Bad Number String!!");
        }
        return toCurrencyFormat(pattern, value);
    }

    /**
     * Use this method like this:<br>
    * <font color="#0000ff">String formatted = Util.formatIntoCurr(123456789.123, 2);<br>
    * formatted ==> "123,456,789.12"
    * </font>
     * @param num <br>
     * @param digits <br>
     * @return formatted String
     */
    public static String formatIntoCurr(double num, int digits) {
        return formatIntoCurr("" + num, digits);
    }

    /**
     * This method actually does all for number formatting into Currency <br>
     * @param pattern <br>
     * @param value <br>
     * @return formatted currency String
     */
    private static String toCurrencyFormat(String pattern, double value) {
        DecimalFormat formatter = new DecimalFormat(pattern);
        return formatter.format(value);
    }

    /**
    * relation fuction to Currency format<br>
    * <font color="#0000ff">String formatted = Util.format("1234.67789", 2);<br>
    * formatted ==> "1234.68"
    * </font>
    * @param String<br>
    * @param int<br>
    * @return formatted double
    */
    public static double format(String str_number, int digits) {
        String pattern = "###.";
        double value = -1.;
        for (int i = 0; i < digits; i++) {
            pattern += "#";
        }
        try {
            value = Double.parseDouble(str_number);
        } catch (NumberFormatException e) {
            System.err.println("[Util] Bad Number String!!");
        }
        return customFormat(pattern, value);
    }

    /**
    * Use this method like this:<br>
    * <font color="#0000ff">String formatted = Util.format(1234.67789, 2);<br>
    * formatted ==> "1234.68"
    * </font>
    * @param numer<br>
    * @param digits<br>
    * @return formatted double
    */
    public static double format(double number, int digits) {
        return format("" + number, digits);
    }

    /**
    * This method actually does all for number formatting...<br>
    * @param pattern pattern to apply to the given double value<br>
    * @param value number to be formatted<br>
    * @return formatted double
    */
    private static double customFormat(String pattern, double value) {
        DecimalFormat myFormatter = new DecimalFormat(pattern);
        try {
            String output = myFormatter.format(value);
            return Double.parseDouble(output);
        } catch (NumberFormatException e) {
            System.err.println("[Util] at customFormat(..): NumberFormatException");
            return -1.0;
        }
    }
}

Related

  1. formatI18N(Object ob)
  2. formatiereSpeichergroesse(long bytes)
  3. formatIncludeCommas(final Number object)
  4. formatInt(final int number)
  5. formatInteger(Object obj)
  6. formatLong(long value)
  7. formatNumber(final long number)
  8. formatNumber(final long number)
  9. formatNumber(int num)