3

For example.

Math.Round(2.314, 2) //2.31
Math.Round(2.301, 2) //2.3   , but I want this as 2.30
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Adrian
  • 836
  • 7
  • 20
  • 44
  • Should be dup of hundred of similar questions. You should use proper format for printing the number (`2.3` and `2.30` is the same value of `float`/`double`/`decimal`) – Alexei Levenkov Oct 23 '13 at 16:27
  • Also this one [Limiting double to 3 decimal places](http://stackoverflow.com/questions/3814190/limiting-double-to-3-decimal-places) explains the issue you have right now. – Alexei Levenkov Oct 23 '13 at 16:32

6 Answers6

10

Numbers don't have any conception of zeroes after a decimal point.

You're actually asking how to convert the number into a string with extra zeroes:

(2.301).ToString("0.00") // "2.30"

See numeric format strings for more detail.
In particular, the 0 specifier will round away from zero.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 3
    I think the OP would still need to call Math.Round first, because ToString will just truncate, i.e. `(2.309).ToString("0.00")` will become `2.30`. – mayabelle Oct 23 '13 at 16:28
  • 3
    @mayabelle: Wrong. (try it) – SLaks Oct 23 '13 at 16:30
  • +1... There should by synonyms search on SO... Build your own truncation title: `{"limit", "keep", "preserve", "leave", "show"} {1,2,3,one, two, three, for} {"decimal places", "after decimal", "digits", "numbers"} in {"float", "double", "decimal", "number", "value", "string"}`... – Alexei Levenkov Oct 23 '13 at 16:36
  • This isn't what the OP needs, but I wish to note that `Decimal` *does* have a loose conception of zeroes after a decimal point, so your leading statement isn't completely true. – Brian Oct 23 '13 at 18:59
4

You want a string formatting of the number:

string val = Math.Round(2.301, 2).ToString("F2");

here's a post on formatting numbers in C#

jltrem
  • 12,124
  • 4
  • 40
  • 50
1

2.3 and 2.30 are the same thing. If you want the string 2.30 then use .ToString("F2") on the Math.Round function.

Lukos
  • 1,826
  • 1
  • 15
  • 29
1

2.3 and 2.30 is the same thing from a code perspective. You can display the trailing zero by formatting a string:

string yourString = Math.Round(2.301, 3).ToString("0.00");
mayabelle
  • 9,804
  • 9
  • 36
  • 59
1

The decimal is still there, you're probably just not seeing because when you look at the string representation, by default it will omit trailing zeros. You can overwrite this behavior by passing a format string to ToString():

Console.WriteLine(Math.Round(2.301, 2).ToString("N2")) // 2.30

But of course, if this is just for display purposes, you don't really need to call Math.Round:

Console.WriteLine(2.301.ToString("N2")) // 2.30

Further Reading

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
0

If you use decimal numbers (their literals end with m, for "money"), you get the behavior you're after. double numbers don't have a concept of significant zeroes the same way that decimals do.

Math.Round(2.314m, 2);
Math.Round(2.301m, 2);

Or if you want to change how you see the numbers, you can use a string format:

Math.Round(2.314, 2).ToString("N2");
Math.Round(2.301, 2).ToString("N2");
Tim S.
  • 55,448
  • 7
  • 96
  • 122