aspose file tools*
The moose likes Java in General and the fly likes How to retain decimal precisions after converting from String to double Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to retain decimal precisions after converting from String to double" Watch "How to retain decimal precisions after converting from String to double" New topic
Author

How to retain decimal precisions after converting from String to double

Nagaraj Shivaklara
Ranch Hand

Joined: Dec 16, 2008
Posts: 75
Hi,

I have a string value "1.0000" and i want to convert it into double to write to the excel. When i convert from string to double, i got value as 1.0; Is there any way that we can retain the precisions same as string after converting from string to double?

Please let me know.


Thanks n Regards,
Nagaraj S K
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

A double is just a number. Numbers don't have intrinsic properties like a number of digits after the decimal point. So you cannot preserve this information when converting a string to a double.

To make Excel format the number in a certain way, you somehow have to set the format options for the cell that contains the number. How you do that depends on what library / API you are using the write the Excel file.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Nagaraj Shivaklara
Ranch Hand

Joined: Dec 16, 2008
Posts: 75
Thank You Jesper,

I am using POI api for excel, currently i am wrting double values to excel as string. Since downstream operations on values is difficult since the values are strings, atleast i want to write as numbers so that i can carry out certain calculations. And we set precisions size to 4 in UI and i need to maintain the same precision in excel also. Is there any way to do that?




Jesper de Jong wrote:A double is just a number. Numbers don't have intrinsic properties like a number of digits after the decimal point. So you cannot preserve this information when converting a string to a double.

To make Excel format the number in a certain way, you somehow have to set the format options for the cell that contains the number. How you do that depends on what library / API you are using the write the Excel file.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Jesper's already told you that
Jesper de Jong wrote:you somehow have to set the format options for the cell that contains the number.


luck, db
There are no new questions, but there may be new answers.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

Sorry, I don't know Apache POI myself so I can't tell you how to format an Excel cell using this API. Looking at the API documentation I see that there are classes named CellFormat and CellNumberFormatter, those are most likely classes you can use to set the format of a cell.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
If you need a certain number of 0s, try BigDecimal, but I don't think you will retain the 0s when you transfer it to Excel. You can of course set Excel to display 4 figures after the decimal point.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to retain decimal precisions after converting from String to double
 
Similar Threads
Convert String into double?
How to covert a String value to double without loosing the precision
BigDecimal
How to avoid exponential in STRING to DOUBLE conversion
java.lang.NumberFormatException: For input string: "139.56"