| Author |
How to retain decimal precisions after converting from String to double
|
Nagaraj Shivaklara
Ranch Hand
Joined: Dec 16, 2008
Posts: 72
|
|
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
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
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: 72
|
|
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: 4163
|
|
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
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
|
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: 32611
|
|
|
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.
|
 |
 |
|
|
subject: How to retain decimal precisions after converting from String to double
|
|
|