Here you can find the source of formatIntoCurr(double num, int digits)
Parameter | Description |
---|---|
num | <br> |
digits | <br> |
public static String formatIntoCurr(double num, int digits)
//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; } } }