June 07, 2009

Converting Double to String without E notation

Last week, I got a task of creating a data loader for one of application. The huge data was presented to me in the form of excel files and for sure there is no better way than using program to read the excel files and load the data to database. I jump started to build the loader application using the Apache POI library. The data types of table schema and data read from the excel didn't match!

There is the trouble: A numeric field in excel files was represented as string in database. Even though this number was not used for calculation, it seems for some reason, the database designer has kept it that way! Trouble started when the POI library's getNumericCellValue() method returned the numbers in the form of scientific notation. The number 12,345,600 would be returned as 123.456E5. But I need it in plain text 1245600 without scientific notation and grouping of numbers!

Solution to this problem lies in configuring NumberFormat or DecimalFormat class found in the package java.text. Here is solution code:

Double comSysNumber = cell.getNumericCellValue();
NumberFormat f = NumberFormat.getInstance();
String refinedNumber = f.format(comSysNumber);
Now the string variable refinedNumber is all set to be stored into my database :-). Hope this becomes useful to others.


Reflections of Geek's Life said...

thanks, your investigation is really helpful :) appreciated.

Maeve said...

This was very useful for me too, thanks!

Vibhas Zanpure said...

Thanks a lot. I had the same problem today while reading an Excel file using Apache POI library. Cheers :-)

Richard Conway said...

Thanks - this was just what I was looking for!

Maddy said...

Thanks for sharing.... :). it's help alot.

Bharaathi said...

Thank u so much.. i helps a lot

Mudassir said...

thanks very much for this post.