Here you can find the source of getNumberComma(String number)
Parameter | Description |
---|---|
String | a parameter |
public static String getNumberComma(String number)
//package com.java2s; /**/*from w w w.ja v a2 s. com*/ * 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; import java.text.NumberFormat; public class Main { /** * Insert comma to String * @param String * @return String */ public static String getNumberComma(String number) { NumberFormat fmt = NumberFormat.getInstance(); String strNumber = fmt.format(Double.valueOf(number)); return strNumber; } /** * Insert comma to Double * @param Double * @return String */ public static String getNumberComma(Double number) { NumberFormat fmt = NumberFormat.getInstance(); String strNumber = fmt.format(number); return strNumber; } /** * Insert comma to int * @param int * @return String */ public static String getNumberComma(int number) { NumberFormat fmt = NumberFormat.getInstance(); String strNumber = fmt.format(number); return strNumber; } /** * 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; } } }