How to format numbers with no grouping separator
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.
Simply:
DecimalFormat dec = new DecimalFormat();
dec.setGroupingUsed(false);
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();
}
}
String result = String.valueOf(number);
returns your number as a string with no formatting.
精彩评论