We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to Convert String to Number for negative values in Java

How to convert string to number for negative values in Java using Aspose.Cells?
Input: For input string: “(123.96)
Expected output: -123.96


Generally, you should change/specify the cell’s number formatting accordingly to display the value in the cell. Could you share your sample Excel file which contains your desired string in some cell and which you want to convert to negative values.

PS. please zip the Excel file prior attaching.

test.zip (2.4 KB)

please check column T
Cell Type is String and one cell value is like “(123.96)”.It is a negative value.
My question is how to convert Text to number format for these type of values(i.e “(123.96)”) for only one column?

Input: “(123.96)”
Expected output: -123.96

can you please help me ? Iam using aspose library for my project and I am in trail period.I am checking the functionalities and I will buy the license.

You can convert Text Numeric Data to Numbers using the function convertStringToNumericValue. Please give it a try and share your feedback.

Workbook workbook = new Workbook("test 2.xls");


Refer to the following article for more information:
Convert Text Numeric Data to Number

but It has to be applied for particular column, not all the columns.
Remaining columns need to String and convert only T column from text to string.
can you please help me in this?

You may set the individual column style number to 0 and try the scenario again.

Workbook workbook = new Workbook("test 2.xls");
Style s = workbook.getWorksheets().get(0).getCells().getColumns().get(19).getStyle();
StyleFlag flag= new StyleFlag();
workbook.getWorksheets().get(0).getCells().getColumns().get(19).applyStyle(s, flag);
workbook.save("test 2Saved.xls");

Another better way is to just browse the T column cells and convert these to string using Cell.putValue() overloads. See the following sample code that works fine for your needs:
Sample code:

Workbook workbook = new Workbook("f:\\files\\Test.xls");
        //Obtaining the reference of the first worksheet
        WorksheetCollection worksheets = workbook.getWorksheets();
        Worksheet sheet =  worksheets.get(0);
        Cells cells = sheet.getCells();

        int col = CellsHelper.columnNameToIndex("T");
        int lRow = cells.getLastDataRow(col);

        for(int r =0;r <= lRow;r++)
            Cell cell = cells.checkCell(r, col);

 if(cell != null)
                cell.putValue(cell.getStringValue(), true);


Hope, this helps a bit.

thank you so much for quick response


Good to know that the suggested code segment works for your needs well. In the event of further queries or issue, feel free to write us back.