Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem while converting String to Bigdecimal

 
Kathiresan Chinnasamy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49457
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 225
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also look at DecimalFormat, with parseBigDecimal set.
 
Kathiresan Chinnasamy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 225
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49457
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49457
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Beckett:
another option may be to use the NumberFormat class . . .
Good idea.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic