Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Big Decimal to String

 
G Burk
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why on earth do you have to use Java1.4?

And welcome to the Ranch
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It even has three setScale methods.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10109
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stephan van Hulst
Bartender
Pie
Posts: 5387
52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is it not backwards compatible?
 
Winston Gutkowski
Bartender
Pie
Posts: 10109
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10109
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic