my dog learned polymorphism*
The moose likes Java in General and the fly likes Convert String to BigDecimal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Convert String to BigDecimal" Watch "Convert String to BigDecimal" New topic
Author

Convert String to BigDecimal

Abhishek Mish
Greenhorn

Joined: Dec 21, 2007
Posts: 29
Hi,
I am trying to convert a String with value as 00000101001 to BigDecimal.
Just using the below code:
String str="00000101001 ";
BigDecimal bd= new BigDecimal (str);
after conversion to BigDecimal i am getting the value as 101001.
How can i get the exact String 00000101001 converted to BigDecimal.

Thanks,
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Actual numbers -- whether they are BigDecmials, ints, longs, floats, bytes, or what-have-you -- never include leading zeros. They have no particular format at all: they represent an abstract mathematical concept. If you want to display leading zeros, you have to provide them yourself.

(Note that in Java, if you write an integer literal with a leading 0, it's assumed to be a number in octal (base 8!))

[Jess in Action][AskingGoodQuestions]
Gareth Lloyd
Greenhorn

Joined: Jun 25, 2009
Posts: 5
Are you trying to convert binary to decimal?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Gareth Lloyd wrote:Are you trying to convert binary to decimal?
In which case a look at the BigInteger constructors might be helpful. Note that is BigInteger, not BigDecimal, but the String you quoted is an integer.
 
Don't get me started about those stupid light bulbs.
 
subject: Convert String to BigDecimal
 
Similar Threads
Trouble with BigDecimal
Precision and scale of a decimal value
Converting to Float
convert String in to String array ???
BigDecimal