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

# Setting a default precision value for Numeric values for the entire spreadsheet?

How can I set the default precision (say I want all non-explicitly formatted numeric values to have a precision of 4 digits after the decimal point)? That is, if the user does a calculation in some cell, if they do not provide an explicit format, then the displayed cell would only show 4 digits precision. But if there are no digits, then leave it as is - for example:

A B

1 13 3

2 16 4.3333 <--- Desired output

* The formula for A2 is =A1 + B1

The formula for B2 is = A1 / B1

How can I replace the default precision?

Hi,

Please consult the following code:

Workbook workbook = new Workbook();
.
.
.

Cells cells = workbook.Worksheets.Cells;
Style style = cells["B2"].GetStyle();
style.Custom = "0.0000";
cells["A1"].SetStyle(style);
workbook.Save("d:\\test\\outputbook.xls");

Thank you.

Hi,

There is more precise way to implement your requirement with minimal of codes.

e.g., The example would implement your desired formattings to the cells having double values in the first worksheet.

Workbook workbook = new Workbook();
workbook.Open("d:\\test\\bktest.xls");
Worksheet worksheet = workbook.Worksheets;
Cells cells = worksheet.Cells;
Style numstyle = workbook.Styles[workbook.Styles.Add()];
numstyle.Custom = "0.0000";
for(int i = 0;i<cells.MaxDataRow;i++)
{
for(int j =0;j<cells.MaxDataColumn;j++)
{
if (cells[i,j].Type == CellValueType.IsNumeric)
{
double doubleValue = cells[i,j].DoubleValue;
if (Math.Ceiling(doubleValue) != doubleValue)
{
cells[i,j].SetStyle(numstyle);
}
}
}
}

workbook.Save("d:\\test\\out_bktest.xls");

Thank you.