Java Fraction Format formatIntoCurr(double num, int digits)

Here you can find the source of formatIntoCurr(double num, 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
num <br>
digits <br>

Return

formatted String

Declaration

public static String formatIntoCurr(double num, int digits) 

Method Source Code

//package com.java2s;
/**/*from ww  w .  ja  va 2  s . co  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. formatFloatToStr(Float str)
  2. FormatFolat(Float num)
  3. formatGetal(float getal)
  4. formatIndexValuePairs(List indicies, List values, String valueSeparator, String rangeSeparator)
  5. formatInt(double value)
  6. formatIntRate(double rateInt)
  7. formatKnots(double v)
  8. formatManeyPattern(String pattern, Double amount)
  9. formatMetricsDecimal(double value)