Formatting positive and negative numbers with C# .NET

Exercises in .NET with Andras Nemes

Let’s say you have the following three numbers:

  • 13.45
  • -32.56

…and you’d like to format them as follows:

  • +13.45
  • -32.56
  • (0)

There’s a special format string that can be used for this purpose. It consists of 3 parts separated by a semi-colon. The first part applies to positive numbers, the second to negative numbers and the third to zeroes:

…and here’s the output:

Three part number format tip

View all various C# language feature related posts here.

View original post 1 more word

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.