5

Possible Duplicate:
Round a double to 2 significant figures after decimal point

I need at most N decimals, no more, but I don't want trailing zeroes. For example, if N = 2 then

15.352
15.355
15.3
15

should become (respectively)

15.35
15.36
15.3
15

Community
  • 1
  • 1
Nikola Novak
  • 4,091
  • 5
  • 24
  • 33
  • 1
    Do you want a third digit of "5" to *always* round up, or round to even? – Jon Skeet Nov 26 '11 at 08:02
  • 2
    Out of interest, what do these numbers represent? It's possible that you should be using `decimal` instead of `double` to start with. – Jon Skeet Nov 26 '11 at 08:22
  • They are values which I use either to draw a graph or display in a table. Additionally I perform some operations on sets of these numbers to get hourly, daily, weekly (etc.) averages, maximums, minimums or totals and then use those values on the graph/table. When drawing a graph I always use `double`, but placing, for example, an averaged value as it is in a table creates an unreadable mess of digits, which is why I need to format them in this way (and each column needs to round to a different number of digits). – Nikola Novak Nov 26 '11 at 09:18
  • But what are the values actually representing? Height? Money? – Jon Skeet Nov 26 '11 at 09:21
  • Currently they're temperatures, percentages (e.g. moisture), amount of rain in a given period, etc. But they can be anything you can put on a graph. – Nikola Novak Nov 26 '11 at 09:32
  • There's a big difference though - temperature makes sense to store in a double, but financial values really *shouldn't* be stored in a double, even though you can put them on a graph... – Jon Skeet Nov 26 '11 at 09:42
  • Thanks. I looked up why (see [link](http://stackoverflow.com/questions/803225/when-should-i-use-double-instead-of-decimal)). I'll keep that in mind if I need to graph anything to do with finances. – Nikola Novak Nov 26 '11 at 09:57

3 Answers3

8

Try Math.Round(value, 2).ToString()

Math.Round(15.352, 2).ToString();  //15.35
Math.Round(15.355, 2).ToString();  //15.36
Math.Round(15.3, 2).ToString();    //15.3
Math.Round(15.0, 2).ToString();    //15

The second paramater for round is for you to specify how many decimal places to round to. It will round up by default.

Gaijinhunter
  • 14,587
  • 4
  • 51
  • 57
5

This can be done by using a custom format string, such as "0.##", which displays a maximum two decimal places.

String.Format("{0:0.##}", 123.4567);      // "123.46"

Reference: http://www.csharp-examples.net/string-format-double/

Andrii Startsev
  • 767
  • 5
  • 11
  • Yes, that does the job for N = 2, but I was hoping there was a way to convert to string for any N. Using, for example double.ToString("N" + X.ToString()), where X is int, will do this, but will also add trailing zeroes which I don't want. – Nikola Novak Nov 26 '11 at 08:12
  • @NikolaNovak is it possible to use decimal instead of double in your case? If so, you can use Decimal.Round(Decimal, Int32) method. http://msdn.microsoft.com/en-gb/library/6be1edhb.aspx – Andrii Startsev Nov 26 '11 at 08:53
  • See my exchange with Jon Skeet in the comments to the question. – Nikola Novak Nov 26 '11 at 09:35
2

Google does lead the way: Use ## to skip leading zeros in your format string.

// max. two decimal places
String.Format("{0:0.##}", 123.4567);      // "123.46"
String.Format("{0:0.##}", 123.4);         // "123.4"
String.Format("{0:0.##}", 123.0);         // "123"
Alois Kraus
  • 13,229
  • 1
  • 38
  • 64