Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I format to only include decimal if there are any

Tags:

c#

.net

What is the best way to format a decimal if I only want decimal displayed if it is not an integer.

Eg:

decimal amount = 1000M
decimal vat = 12.50M

When formatted I want:

Amount: 1000 (not 1000.0000)
Vat: 12.5 (not 12.50)
like image 432
Thomas Jespersen Avatar asked Oct 19 '08 15:10

Thomas Jespersen


People also ask

How do you get only the decimal part of a number in Excel?

Select a cell and type this formula =A1-TRUNC(A1) (A1 is the cell you want to extract decimal value from) into the Formula Bar, and then press Enter key. Keep selecting the first result cell, and drag fill handle down to get all results. You can see the decimal values are extracted with sign as below screenshot shown.

How do you restrict decimals?

Now you can limit the decimal places. Select the number cell and in the Menu, go to Format > Number > More Formats > Custom number format.


1 Answers

    decimal one = 1000M;
    decimal two = 12.5M;

    Console.WriteLine(one.ToString("0.##"));
    Console.WriteLine(two.ToString("0.##"));
like image 145
Richard Nienaber Avatar answered Oct 25 '22 12:10

Richard Nienaber