• Post Reply Bookmark Topic Watch Topic
  • New Topic
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Unable to write Double value with out scientific notation

 
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I see that there are several threads already in this forum, that talks about Double and Scientic notation of them and the writing them as string. But I could not get much help from them.

We need to write some amount returned from database to a file seperated by comma. We do not know, how may digits after decimal or before and neither we need to format them to a particular pattern. All we need to do is to write what ever value we get from DB.

So, while testing, we got couple of data like 1.3 and 10426000. So the idea is to write them as it is to a file.

We tried few things, but one worked for 1.3 but failed for ther other and vice versa.

Our code is similar as below


Basically, If I store the double value to a BigDecimal and then invoke toString() is working like a charm and printing the actual values.

However, in reality, after we get the data from DB, they are stored as a property in an object, as Double. The moment it is converted to Double , toString() on such Double prints scientific notation for 10426000 , but 1.3 is fine.

Here we have two options,
we need to change the datatype of that property to BigDecimal, which may trigger issues in another programs that use this object. Or in another way, we worked around the problem, as below, we continue storing the value as Double in the property. But when we need to write to file, we are creating a BigDecimal out of that double value and then adding another BigDecimal("0.0") and then writing.

Like


Though the above logic worked, we particularly did not like it. There should be some simple way, which we are forgetting to print the value directly from Double, without scientifc notation.

Any suggestions.
 
Sheriff
Posts: 24635
56
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kumar Raja wrote:There should be some simple way, which we are forgetting to print the value directly from Double, without scientifc notation.



Well you're right, there is. It's called DecimalFormat -- but I see you already know about it. What problems did you encounter when you tried to use it on a Double object?
 
Marshal
Posts: 65425
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is wrong with using printf and the % tags?
 
Kumar Raja
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul, I used DecimalFormat, without specifying any specific pattern, and I end up getting comma in the value, which I do not want.and then I tried to give a pattern, but then the value is formated to that pattern as required but if the actually value is not fitting into that pattern, then the values are truncated.

Campbell, you are right. printf statement should have helped me. I never thought about that at first place. Let me try it.

Thanks for the suggestions


Hi Campbell, I tried to use String.format(format,args) and also String.out.printf() methods to format and use %f to format the double, without specifying the precision, so as per the documentation given here


'f' '\u0066' Requires the output to be formatted using decimal format. The localization algorithm is applied.
The result is a string that represents the sign and magnitude (absolute value) of the argument. The formatting of the sign is described in the localization algorithm. The formatting of the magnitude m depends upon its value.

If m NaN or infinite, the literal strings "NaN" or "Infinity", respectively, will be output. These values are not localized.

The magnitude is formatted as the integer part of m, with no leading zeroes, followed by the decimal separator followed by one or more decimal digits representing the fractional part of m.

The number of digits in the result for the fractional part of m or a is equal to the precision. If the precision is not specified then the default value is 6. If the precision is less than the number of digits which would appear after the decimal point in the string returned by Float.toString(float) or Double.toString(double) respectively, then the value will be rounded using the round half up algorithm. Otherwise, zeros may be appended to reach the precision. For a canonical representation of the value, use Float.toString(float) or Double.toString(double) as appropriate.



precision is defaulted to 6, again which is not the requirement. I should be able to print exactly what ever I get, nothing else to be added or rounded up.

I tried to search for any flags that can be used along with %f, some thing like find out the exact digits after decimal in the actual value to be formatted instead of defaulting to 6 precision. But I don't think there is any such flag.


 
Campbell Ritchie
Marshal
Posts: 65425
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kumar Raja wrote: . . . I tried to search for any flags that can be used along with %f, . . . . But I don't think there is any such flag.


Where did you look? Obviously not here, nor here, nor here, because you would have found the options quickly enough there.
 
Kumar Raja
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,

I did go through the links you provided and I understand the flags that can be used along with %f. However, I still need to specify the precision, otherwise I get a precision of 6 (defaulted to 0 if no value after decimal) as given in the document.

For eg, if I had to try displaying

Double d1 = 10426000d;
Double d2 = 10426000000d;
Double d3 = 1.32;
Double d4 = 1000000000.00000000009d;
Double d5 = 1000000000.000001d;
Double d6 = 1.234E3;

I tried to use DecimalFormat with pattern "#.#" and this works correctly for d1,d2,d6 but not for d3,d4,d5.

if I use #.## then the d1,d2,d3,d6 works but not d3,d4.

What if d3 = 1.323 then I the above format prings only 1.32.

I also looked into the flags available with printf methods, based on syntanx %[argument_index$][flags][width][.precision]conversion

I tried with all the flags -,#,0 but none of them could result in what I'm looking for, i.e to print the value as it is without any extra padding.


 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kumar Raja wrote:I tried with all the flags -,#,0 but none of them could result in what I'm looking for, i.e to print the value as it is without any extra padding.


Actually, I think your original idea was probably closest to what you want - ie, convert to BigDecimal. But then you need to use the toPlainString() method. To my knowledge, it's the only example of a class being changed after the fact in a non-backward compatible way. Before version 1.5, toString() used to work exactly like toPlainString() does now.

My advice - try:
Winston
 
Don't sweat petty things, or pet sweaty things. But cuddle this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!