Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format numbers with no grouping separator

Tags:

I'm trying to format a BigDecimal value by using methods of DecimalFormat.format().

My problem, is I don't know how to set DecimalFormats DecimalFormatSymbol to format text without any grouping separators.

I'd like to know how to set a grouping symbol and use the format methods. I know how to do it differently by using replace or others methods but it's not what I want.

So I need to know how to set an empty character as grouping operator.

Example:

DecimalFormat dec = new DecimalFormat(); DecimalFormatSymbols decFS = new DecimalFormatSymbols(); decFS.setGroupingSeparator( '\0' ); dec.setDecimalFormatSymbols( decFS ); BigDecimal number = new BigDecimal(1000); String result = dec.format(number); 

I want to get "1000" as string with no other characters. Please help

note(react to post): I want to formate the number only, without grouping.

like image 546
Perlos Avatar asked Jul 14 '11 13:07

Perlos


People also ask

What is a grouping separator?

In many programming languages, the thousands separator (e.g., the "," in the American string "1,000") is called the "grouping separator". Why is this? Are there any real-world locales that separate written integers on some other boundary? Do people somewhere write numbers like 86,75,30,9 or 8675,309?

How to change decimal separator Java?

You can use the DecimalFormatSymbols class to change the symbols that appear in the formatted numbers produced by the format method. These symbols include the decimal separator, the grouping separator, the minus sign, and the percent sign, among others.

What is locale formatted number?

NumberFormat is a Java class for formatting and parsing numbers. With NumberFormat , we can format and parse numbers for any locale. NumberFormat allows us to round values, set decimal separators, set the number of fraction digits, or format values according to a specific locale.

What is decimal format?

DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. It has a variety of features designed to make it possible to parse and format numbers in any locale, including support for Western, Arabic, and Indic digits.


2 Answers

Simply:

 DecimalFormat dec = new DecimalFormat();       dec.setGroupingUsed(false); 
like image 70
powerMicha Avatar answered Sep 17 '22 15:09

powerMicha


If you dont want any formatting marks in the number you should just call toString on the BigDecimal Object.

import java.math.*;  public class TestBigDecimal {     public static void main(String[] args)     {         BigDecimal number = new BigDecimal(1000);         String result = number.toString();     } } 
like image 22
Hunter McMillen Avatar answered Sep 18 '22 15:09

Hunter McMillen