We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Excel number format

Hi,
I am formatting numbers in an excel cell and I wanted to add "," seprator and $ sign, so this peice of code worked.

IWorksheet sheet = workbook.Worksheets[0];
sheet.Range["A1:C20"].NumberFormat = "$###,###";

I want to format the negative numbers in excel cells as () as opposed to the - sign. How can I acheive this?

SO to be clear I want to display

123456 as $123,456 (which works)
-123456 as $(123,456). Right now I can only get -$123,456 with the above.

Thanks.


7 Replies

GM Geetha M Syncfusion Team March 30, 2010 04:43 AM UTC

Hi Srinivasa,

You may achieve $123,456 and $(123,456) for 123456 and -123456 by using the numbed format below.

_($* #,##0_);_($* (#,##0);_($* "-"_);_(@_)

Please try this and let me know if you have any questions.

Regards,
Geetha


SI Srinivasa Igur March 30, 2010 01:41 PM UTC

Thanks. That works however I had to change the - from " " to ' '

I have one more question. How do I apply this to a dynamic range in a worksheet. I do not want to choose the range as (A1:ZZ65000). Also how can I apply this to a column?

Thanks again.


SI Srinivasa Igur March 30, 2010 01:48 PM UTC

oops. The $ sign is left justified and seperated from the amount like shown below:

$ 123.456
$ (123,456)


GM Geetha M Syncfusion Team March 31, 2010 06:02 AM UTC

Hi Srinivasa,

I see the problem.

Here is the modified format that displays without space and alignment

_($#,##0_);_($(#,##0);_($* '-'_);_(@_)

Please try this and let me know if you have any questions.

Regards,
Geetha


GM Geetha M Syncfusion Team March 31, 2010 06:11 AM UTC

Hi Srinivasa,

Regarding applying format to a dynamic range, you can have it as a style and apply to any row or column.

IStyle numberFormat = workBook.Styles.Add("NumberFormatStyle");
numberFormat.NumberFormat = "_($#,##0_);_($(#,##0);_($* '-'_);_(@_)";

sheet.SetDefaultColumnStyle(1, numberFormat);

Please refer to our documentation in the link below.

http://help.syncfusion.com/ug_81/Reporting_XlsIO/DefaultStyles.html

Regards,
Geetha


SI Srinivasa Igur March 31, 2010 01:14 PM UTC

Thanks. This works perfectly.


GM Geetha M Syncfusion Team April 1, 2010 07:14 AM UTC

Hi Srinivasa,

Thank you for the update.

Regards,
Geetha

Loader.
Live Chat Icon For mobile
Up arrow icon