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)
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.
Now you can limit the decimal places. Select the number cell and in the Menu, go to Format > Number > More Formats > Custom number format.
decimal one = 1000M;
decimal two = 12.5M;
Console.WriteLine(one.ToString("0.##"));
Console.WriteLine(two.ToString("0.##"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With