This project has moved and is read-only. For the latest updates, please go here.

NumberFormat with only one decimal

Mar 7, 2012 at 5:00 PM
Edited Mar 7, 2012 at 5:05 PM

Hello everyone,

I have a problem in using the numberformat property in order to round cell values to only one decimal.

Here is my approach to apply the numberformat to one cell :

worksheet.Cells[r, c].Style.Numberformat.Format ="#,##0.0";

An ArgumentOutOfRangeException is thrown where I use this function :

workbook.Worksheets.Add(worksheet.Name, worksheet);

The problem is that if I set two decimals (like "#,##0.00") or zero (like"#,##0"), there is no exception!
I know that 0 and 2 decimals take part from the standart list of numberformats in an Excel worksheet, so it could be the explanation... But how to allow this numberformat of only one decimal without exception ?

Thank you for your answers ! :)

Mar 7, 2012 at 5:36 PM

Did you try "#,#0.0" ?

Mar 7, 2012 at 5:43 PM
Edited Mar 7, 2012 at 5:43 PM

Doesn't work...

I try this :
- Create a template with this special numberformat in his style, get style in code in my new workbook from this template, update the style. After that, I can see the new numbertformat (#,##0.0) in my workbook. But... Same error when adding the worksheet to the workbook...

- Create a template and apply directly in Excel the numberformat in cells that I want to be round. Same error...

I really don't know how to process now :/

Mar 7, 2012 at 6:56 PM

hum, in my code "#,#0.0" works fine but I am using an older version (2.9.x.x). Maybe it is an issue related with new version. I will update my project to test.

Mar 8, 2012 at 8:31 AM


Do you have some news about it ? I will test with another version soon, i'll take you informed. Btw, thank you for your help!

Mar 8, 2012 at 12:42 PM
Edited Mar 8, 2012 at 12:43 PM

My code still works fine in the newer version. I could post my code here but I dont think it will be helpful

Mar 8, 2012 at 3:21 PM

Work perfectly with the version 3, thank you very much :)

Just one last question, which numberformat do you use for percent formating ? "0%" doesn't work for me :/

Mar 8, 2012 at 3:59 PM

ws.Cells[r, c + 8].Style.Numberformat.Format = "#%";
ws.Cells[r, c + 8].Value = (double)subAval.peso / 100;

Mar 8, 2012 at 4:16 PM

Thank you!