# [ACCEPTED]-How do I format to only include decimal if there are any-.net

Accepted answer

```
decimal one = 1000M;
decimal two = 12.5M;
Console.WriteLine(one.ToString("0.##"));
Console.WriteLine(two.ToString("0.##"));
```

0

**Updated following comment by user1676558**

Try this:

```
decimal one = 1000M;
decimal two = 12.5M;
decimal three = 12.567M;
Console.WriteLine(one.ToString("G"));
Console.WriteLine(two.ToString("G"));
Console.WriteLine(three.ToString("G"));
```

For a decimal value, the default 9 precision for the "G" format specifier 8 is 29 digits, and fixed-point notation 7 is always used when the precision is omitted, so 6 this is the same as "0.#############################".

Unlike 5 "0.##" it will display all significant 4 decimal places (a decimal value can not 3 have more than 29 decimal places).

The "G29" format 2 specifier is similar but can use scientific 1 notation if more compact (see Standard numeric format strings).

Thus:

```
decimal d = 0.0000000000000000000012M;
Console.WriteLine(d.ToString("G")); // Uses fixed-point notation
Console.WriteLine(d.ToString("G29"); // Uses scientific notation
```

Source:
stackoverflow.com

More Related questions

Cookie Warning

We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.