• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem while converting String to Bigdecimal

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 225
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Beckett:
another option may be to use the NumberFormat class . . .

Good idea.
 
Lookout! Runaway whale! Hide behind this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic