jQuery in Action, 2nd edition*
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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript 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: 77
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: 14114
    
  16

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: 77
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: 4530
    
    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: 14114
    
  16

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: 38466
    
  23
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