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

# Convert String to BigDecimal

Greenhorn
Posts: 29
• Number of slices to send:
Optional 'thank-you' note:
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,

author and iconoclast
Posts: 24207
46
• Number of slices to send:
Optional 'thank-you' note:
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!))

Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:
Are you trying to convert binary to decimal?

Marshal
Posts: 79514
379
• Number of slices to send:
Optional 'thank-you' note:

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.