Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Decimal format like 2.07619048e-05

 
Richard Cook
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I wonder if you could help me, I have to output an XML file which has lots of decimal values at a certain length.

I have tried DecimalFormat, but I think I am loosing precision, i'm not sure.

The format required is: 2.07619048e-05 etc with all values the same length.

Please could somebody help as I am pulling my hair out!

Thanks in advance.


Regards




Richard
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15435
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In what kind of way is your data stored, as float or double values, or something else?

Can you tell us in more detail (with some code, if possible) what you tried with DecimalFormat and why exactly you think you are loosing precision (do you have an example which shows this)?
 
Richard Cook
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jasper,

Thank you for your prompt reply.

The values are coefficients calculated on the fly using doubles.

The DevimalFormat code is as follows:


And it is currently called like:


The format I am trying to match is as follows:


But I am getting the following output (ignoring indentation):


I am not completely sure if I am loosing precision, but I would prefer if the values were all in the same format as the above example.

Many thanks


Richard

 
Richard Cook
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was also using:


I also changed the 00 for hashes.

Regards



Richard
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this help ?
 
Richard Cook
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,

I have just tested that with various long numbers and it is spot on, thank you so much.


Regards



Richard
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Cook wrote:

I have just tested that with various long numbers and it is spot on, thank you so much.


I'm sceptical of your requirement for such a rigid format inside the XML. XML holds data and should not really care about the formatting of the values it holds. Only views of the XML data should be concerned with the format of displayed numbers.
 
Richard Cook
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,

The reason behind this is the XML is imported in a (not very well written) system, and it was falling over with length issues, hence I wanted to get it as close to the original template as possible.

Thanks again


Richard
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic