42

For 10 I want 10 and not 10.00 For 10.11 I want 10.11

Is this possible without code? i.e. by specifying a format string alone simlar to {0:N2}

RBT
  • 21,293
  • 19
  • 144
  • 210
Neil
  • 675
  • 2
  • 6
  • 12
  • @RyanGates, duplicate of a newer question? :P – Andrew Oct 16 '15 at 22:49
  • My suggestion is this one: http://stackoverflow.com/a/33180829/2321042 – Andrew Oct 16 '15 at 23:07
  • 1
    @Andrew An older question can be a duplicate of a newer question provided that there are better answers. http://meta.stackoverflow.com/q/251938/299327 – Ryan Gates Oct 19 '15 at 14:07
  • 1
    @RyanGates, thanks for that! When flagging, it just says "This question has been asked before and already has an answer". I agree the other thread is more complete than this one. :) – Andrew Oct 19 '15 at 16:18

3 Answers3

48
decimal num = 10.11M;

Console.WriteLine( num.ToString( "0.##" ) );
tvanfosson
  • 509,016
  • 97
  • 693
  • 791
  • 3
    This doesn't work - decimal num = 10.1; The best I can come up with is: num .ToString("C").Replace(".00", ""); Anyone who has the answer for this case would be helping me! – Dave Bish May 24 '11 at 17:53
  • @Dave - what doesn't work? Is it that you want exactly 2 decimals unless they are zero? – tvanfosson May 24 '11 at 18:11
  • 5
    @Dave - How about `num % 1 == 0 ? num.ToString("0") : num.ToString("0.00");` You could implement it as an extension method on decimal. – tvanfosson May 30 '11 at 20:45
  • Console.WriteLine( num.ToString( "0.00" ) ); forces 2 DP even if the value is zero. i..e "0.00" or "0.10" etc. Don't forget default behavior is to round up from .005 not down. – Microsoft Developer Apr 10 '14 at 11:27
  • @dotNETNinja that's what Dave was asking for. In retrospect I might do `decimal.Truncate(num) == num` instead of mod by 1, though. – tvanfosson Apr 10 '14 at 13:02
  • this worked for me under mono... if you really have to use string operations to get this right, don't forget string.trimRight(new char[]{'0'}) to trim any trailing zeros - but do check that there is a decimal separator first – Radu Simionescu Mar 31 '15 at 19:15
3

It seems to me that the decimal precision is intrinsic to the decimal type, which defaults to 4 decimal places. If I use the following code:

decimal value = 8.3475M;
Console.WriteLine(value);
decimal newValue = decimal.Round(value, 2);
Console.WriteLine(newValue);

The output is:

8.3475
8.35
GeoffDev
  • 55
  • 2
0

This can be achieved using CultureInfo. Use the below using statement to import the library.

using System.Globalization;

For the decimal conversion, ## can be used for optional decimal places and 00 can be used for mandetory decimal places. Check the below examples

double d1 = 12.12;
Console.WriteLine("Double :" + d1.ToString("#,##0.##", new CultureInfo("en-US")));

String str= "12.09";
Console.WriteLine("String :" + Convert.ToDouble(str).ToString("#,##0.00", new CultureInfo("en-US")));

String str2 = "12.10";
Console.WriteLine("String2 with ## :" + Convert.ToDouble(str2).ToString("#,##0.##", new CultureInfo("en-US")));
Console.WriteLine("String2 with 00 :" + Convert.ToDouble(str2).ToString("#,##0.00", new CultureInfo("en-US")));


int integ = 2;
Console.WriteLine("Integer :" + Convert.ToDouble(integ).ToString("#,##0.00", new CultureInfo("en-US")));

the results are as follows

Double :12.12
String :12.09
String2 with ## :12.1
String2 with 00 :12.10
Integer :2.00
Mohamed
  • 65
  • 8