File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Big Decimal to String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Big Decimal to String" Watch "Big Decimal to String" New topic
Author

Big Decimal to String

G Burk
Greenhorn

Joined: Nov 17, 2011
Posts: 1
I have spent way too many hours searching the internets trying to find a way to convert a Big Decimal Value to a String and retain the decimal as well as the numbers that follow it.

I have to live in the 1.4 world for this project so something like

BigDecimal sysid = new BigDecimal(50000365566.0000);

System.out.println( String.format("%09.4f", sysid));

isn't supported in 1.4

And unfortunately the simple toString() method always chops off the decimal and training numbers.

This seems like such a simple problem but it has me totally stumped (not a hard feat since I am new to Java). Any one have a suggestion or a like I can follow?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Is this present in 1.4? http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html#format(java.lang.Object,%20java.lang.StringBuffer,%20java.text.FieldPosition)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

Or create the BigDecimal from a String: Not only will this maintain the trailing zeros (also in toString()), but it will also ensure that the exact value is stored. When you create a BigDecimal from a double, that double may already be imprecise (even if you hard-code its value). You don't have this problem when creating BigDecimals from Strings. See our Beginner's FAQ, item #20 for more information.

An example of what I meant:
You'll see that the first two printed values are not what you specified they should be.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39788
    
  28
Why on earth do you have to use Java1.4?

And welcome to the Ranch
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Rob Spoor wrote:Or create the BigDecimal from a String:


Excellent advice. Must've had my brain turned off. Doesn't BD have also setScale() or somesuch method that controls the precision both for storage and for display?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

It even has three setScale methods.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

In Java 1.4, there are only 2 setScale methods:

BigDecimal setScale(int scale)
BigDecimal setScale(int scale, int roundingMode)

Always best to use setScale(int, int).
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8195
    
  23

G Burk wrote:I have to live in the 1.4 world for this project...

As others have asked: Why?

One other point. If and when you do upgrade, you will also have to change the output command, because the equivalent of BigDecimal.toString() in 1.4 is toPlainString() in 1.5 onwards - a very rare case of Java not being backwards-compatible.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

How is it not backwards compatible?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8195
    
  23

Stephan van Hulst wrote:How is it not backwards compatible?

Because toString() produces different results in 1.4 and 1.5. I don't know about you, but that's not backwards compatible to me (and the "new look" came as a bit of a shock, I can tell you; I have a fair few classes that relied on the old one).

I suspect they wanted to standardize the toString() format for numeric classes; but to me, 'backwards compatible' would have been adding a 'toNumberString()' method that produces the new format.

Winston
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

Well, unless the format of the toString() result was documented, its return value is an implementation detail and can be changed as desired. However, it does seem to be documented. From the Java 1.4 Javadocs:
Returns the string representation of this BigDecimal. The digit-to- character mapping provided by Character.forDigit(int, int) is used. A leading minus sign is used to indicate sign, and the number of digits to the right of the decimal point is used to indicate scale. (This representation is compatible with the (String) constructor.)

In Java 5, that documentation significantly changed, and I think there is indeed a lack of backwards compatibility here.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8195
    
  23

Rob Spoor wrote:Well, unless the format of the toString() result was documented, its return value is an implementation detail and can be changed as desired...

True, but I use BigInteger/BigDecimal more than most I suspect, and I was pretty sure it was (thanks for the confirmation, BTW).
It's also the only time I can ever remember them making a change like that, so I can't complain too much.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Big Decimal to String