Free Support Forum - aspose.com

Get cell contents as String without formatting

Hi Team,
We have value 1234567.0375395 when we try to use below methods it return the values as listed below
cell.getStringValueWithoutFormat() --> 1234567.0375394989
cell.getStringValue() --> 1234567.04
cell.getValue() --> 1234567.0375394989
cell.getDoubleValue() --> 1234567.0375394989

we even tried with current version 21.5 but we could not get the expected value 1234567.0375395. Kindly suggest.

@Javith,
We have tried the following sample code with the latest version but could not observe this issue. Please share your environment details with us for our analysis.

Workbook workbook = **new** Workbook("Book1.xlsx");
//workbook.getWorksheets().get(0).getCells().get("A1").setValue(1234567.0375395);
Cell cell = workbook.getWorksheets().get(0).getCells().get("A1");
System.out.println(cell.getStringValueWithoutFormat());//=>1234567.0375395
System.out.println(cell.getStringValue());//=>1234567.038
System.out.println(cell.getValue());//=>1234567.0375395
System.out.println(cell.getDoubleValue());//=>1234567.0375395
System.out.println(cell.getDisplayStringValue());//=>1234567.038 

Book1.xlsx.zip (6.4 KB)

@ahsan
Below are the environment details

  1. windows 10
  2. JDK 1.8.0_231
  3. aspose-cells-21.5.jar

could not able to download template shared , getting below message.
“Sorry, this file is private. Only visible to topic owner and staff members.”
Can you update the number to 1513628.0375395 and try again with the same code.

@Javith,

I am trying it in a similar environment except Windows 10 as I have Windows 7.

It is a simple Excel file containing only one value in cell A1. You may create your own file similar to this.

Here is the program output having sample value 1513628.0375395.

1513628.0375395
1513628.038
1513628.0375395
1513628.0375395
1513628.038

@Javith,
For numeric values, Cell.StringValueWithoutFormat just gives the string of the numeric value kept in data model, such as by Double.toString().

We think you may want to get the string of the double value with maximum digits like what is shown in ms excel. If so, please format the numeric value with your custom format pattern. For ms excel, commonly the maximum precision for numeric values is 15 digits.

Because Cell.StringValueWithoutFormat leads to ambiguity for users, we have made it obsolete in recent versions and will remove it later in this year. Please format double values with your own format pattern according to your requirement instead. For cells, maybe you need to check whether the value in data model is numeric firstly, Cell.IsNumericValue should be helpful for such situation.

Hi,

I tried with new value 5589.39608967839 using the below code and still the result is not accurate.
System.out.println(cell.getStringValueWithoutFormat()); //5589.396089678397
System.out.println(cell.getStringValue()); //5589.3960896784
System.out.println(cell.getDoubleValue()); //5589.396089678397
System.out.println(cell.getValue()); //5589.396089678397

I could not able to find method Cell.IsNumericValue in latest aspose cells version 21.5.
I even try to use custom format for the cell using the below code but still it is not correct.
Style st = cell.getStyle();
st.setCustom("#.##############");
cell.setStyle(st);
StyleFlag flg = new StyleFlag();
flg.setNumberFormat(true);
cell.setStyle(st, flg);

Kindly helps us to get the correct value 5589.39608967839 using the suitable method.
Please try to simulate the issue from your side using windows and jdk1.8.0_231.

Thanks
Javith

@Javith,

For the method IsNumericValue, it is added from our recent fixes (e.g. 21.5.4), so please let user try our latest fix:
aspose-cells-21.5.6-java.zip (7.3 MB)

We recommended you should use the pattern to format the numeric value by yourselves. For Java program, you should get the double value from a cell, then build your own DecimalFormat with the pattern and use it to format the double value accordingly.

Anyways, if you still find any issue, kindly do share your exact code (runnable) to trace the issue. We will check your issue soon.

@Amjad,

Can you provide a sample code as per your recommedation. so that i can try the same. i already tried using the pattern and it didn’t worked.

@Javith,
Here is a sample code recommended by @Amjad_Sahi.

Workbook workbook = new Workbook();
workbook.getWorksheets().get(0).getCells().get("A1").setValue(5589.39608967839);
Cell cell = workbook.getWorksheets().get(0).getCells().get("A1");
Double value = cell.getDoubleValue();
NumberFormat formatter = new DecimalFormat("#0.###########");
System.out.println(formatter.format(value));