Results 1 to 4 of 4
- 09-28-2011, 10:42 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 10
- Rep Power
- 0
More sensible BigDecimal toString()?
The standard behaviour of BigDecimal.toString() makes no sense to me. In the JavaDoc you see
[123, -1] "1.23E+3"
Why would you ever want to show 1,230 as 1.23E+3?
Is there a simple way to convert a BigDecimal to a string such that scientific notation is used if the exponent is bigger/smaller than a certain value, like a pocket calculator does?
-
Re: More sensible BigDecimal toString()?
Your best bet for questions like these is to first check the API as it often will solve this problem a lot faster than we can. And in fact it does! Check out a method of BigDecimal that begins with toPlain...() in the BigDecimal API. You'll have to supply your own program logic to decide which method to call.
- 09-29-2011, 02:49 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: More sensible BigDecimal toString()?
The API also describes the sense behind the current (>1.4) toString() behaviour. It may not be what you want in some context, but it is as sensible as it could be.
- 09-29-2011, 04:08 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 10
- Rep Power
- 0
Re: More sensible BigDecimal toString()?
I'm puzzled why there is toPlainString and toEngineeringString but no toScientificString?
toString doesn't guarantee producing scientific notation, so there are some numbers where you don't have the option of scientific format. OK, probably not numbers which you would want in that format.
In practical terms, if I wanted scientific notation for numbers outside, say 10^8 and 10^-8, is toString always going to do that?
Whether the behaviour of toString is as sensible as it could be is a matter of opinion - they have designed to be reversible and locale independent, but at the expense of readability and general usefulness IMHO.
Similar Threads
-
Negating a BigDecimal
By ryanmk54 in forum New To JavaReplies: 5Last Post: 05-16-2011, 06:23 PM -
Divide bigdecimal again
By ellhar in forum New To JavaReplies: 3Last Post: 03-23-2011, 10:19 AM -
BigDecimal and BigInteger
By jon80 in forum New To JavaReplies: 8Last Post: 11-28-2010, 11:15 AM -
Using BigDecimal
By doymand in forum New To JavaReplies: 2Last Post: 10-30-2010, 09:25 AM -
BigDecimal question
By orchid in forum New To JavaReplies: 2Last Post: 08-12-2008, 01:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks