| Author |
Problem while converting String to Bigdecimal
|
Kathiresan Chinnasamy
Ranch Hand
Joined: Jan 01, 2008
Posts: 65
|
|
Hi Friends, Can you suggest me for this problem? Problem : String s = "123,10"; Bigdecimal decimal = new Bigdecimal(s); I am getting Exception while execute this statement : The Exception is NumberFormatException How to resolve this? is there any format to specify for delimiter such as , or any special char Thanks in advance
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32675
|
|
|
Go through the API documentation for the BigDecimal constructor, and you get the grammar in BNF (Backus-Naur Format). It does appear to require a "." character rather than "," so it would appear that changing the Locale won't help. You are going to have to try the replace or replaceAll methods of String.
|
 |
Carey Evans
Ranch Hand
Joined: May 27, 2008
Posts: 225
|
|
|
You can also look at DecimalFormat, with parseBigDecimal set.
|
 |
Kathiresan Chinnasamy
Ranch Hand
Joined: Jan 01, 2008
Posts: 65
|
|
thank you Campbell Ritchie and Carey Evans --------------------------------------------------- Go through the API documentation for the BigDecimal constructor, and you get the grammar in BNF (Backus-Naur Format). It does appear to require a "." character rather than "," -------------------------- i couldn't able to understand this solution... so please give me more elaborate ---------------------------------------------------------- You can also look at DecimalFormat, with parseBigDecimal set. ------------------------------------------------------------- I am using jdk 1.4 version so its not supporting could you give me the sample ... important: this conversion for italy euro
|
 |
Carey Evans
Ranch Hand
Joined: May 27, 2008
Posts: 225
|
|
Campbell was referring to the precise description in the BigDecimal constructor JavaDoc. You must use a decimal point, not a comma, to separate the integer and fractional parts of the value passed to BigDecimal. Since you can�t use the new DecimalFormat support, you�ll have to replace the comma by a decimal point before converting the string to a BigDecimal.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32675
|
|
Originally posted by Carey Evans: Campbell was referring to the precise description . . .
Exactly right, but expressed better than I put it BTW: If you post in the advanced forum, Kathiseran, we assume you are no longer a beginner, so we can use more complicated explanations.
|
 |
Paul Beckett
Ranch Hand
Joined: Jun 14, 2008
Posts: 95
|
|
another option may be to use the NumberFormat class to parse the String into a Number. You should be able to construct a BigDecimal from the Number (or its primitive equivalent). The NumberFormat class has methods such as getXXXInstance with overloaded versions that will allow you to use a Locale as an argument. I think the Italy/Italian locale uses the "," instead of ".". Using the API docs for the NumberFormat/Locale/BigDecimal classes you should be able to find what you are looking for. [ July 14, 2008: Message edited by: Paul Beckett ]
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32675
|
|
Originally posted by Paul Beckett: another option may be to use the NumberFormat class . . .
Good idea.
|
 |
 |
|
|
subject: Problem while converting String to Bigdecimal
|
|
|