Custom Number Format with Percentage using C#

I need to set custom number format with percentage using C#. I want value inside cells get multiplied by default with custom format #,##0.0% in the cells.
for ex:
value = 52.86;
will become 5286.0%.
my requirement is I don’t want it to get multiplied.

please help me with this.

@rohan0910,

This is due to your value, i.e., “52.86”. You can confirm it in MS Excel manually and you will get same results as Aspose.Cells gives. The value should be inserted as “0.5286”, then your custom formatting (percentage) would take affect in accordance with your desired needs.

PS. if you have existing values, may be you could divide the value by 100 to get desired results (after applying custom formattings).

1 Like

If I take the format as “#,##0.0/%”, it works in Excel.
But tried programmatically then got an error.

@rohan0910,

When I directly set the custom formatting “#,##0.0/%” for your value in MS Excel, it gives error.

@rohan0910,

If you don’t want % to be escaped, please use backslashes and double quotes. See following sample code:

Sample Code to Set Custom Number Format with Percentage using C#

Workbook workbook = new Workbook();
Cell cell = workbook.Worksheets[0].Cells["A1"];
cell.PutValue(52.86);
Style style = cell.GetStyle();
style.Custom = @"#,##0.00%";
cell.SetStyle(style);
Console.WriteLine(cell.StringValue);

cell = workbook.Worksheets[0].Cells["A2"];
cell.PutValue(52.86);
style = cell.GetStyle();
style.Custom = @"#,##0.00\%";
cell.SetStyle(style);
Console.WriteLine(cell.StringValue);

cell = workbook.Worksheets[0].Cells["A3"];
cell.PutValue(52.86);
style = cell.GetStyle();
style.Custom = "#,##0.00\"%\"";
cell.SetStyle(style);
Console.WriteLine(cell.StringValue);
workbook.Save(dir +"dest.xlsx");