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?

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.

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.

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.

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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com

subject: How to retain decimal precisions after converting from String to double