This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to retain decimal precisions after converting from String to double

 
Nagaraj Shivaklara
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15367
40
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nagaraj Shivaklara
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15367
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic