Need to format a nullable decimal in C#

Mannsi
Mannsi used Ask the Experts™
on
Hello experts,

I'm having what I consider a silly problem. I have a decimal? value in c# that I would like to format. It currently displays as 40.00000 but I would like to see 40.00. If the decimal value happens to be null I would like to see either a 0 or nothing.

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Billing Engineer
Most Valuable Expert 2014
Top Expert 2009
Commented:
apart from the nullable:
http://blog.stevex.net/index.php/string-formatting-in-csharp/

so, I would suggest:
string result = value == null ? "0" : String.Format("{0:0.00}", value);

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial