0

I need the number returned to have exactly 2 decimal points but I can't seem to get this to work.

Is this something built into C#?

I also tried setting the number to a double and a decimal and I get the same answer both times.

price = Math.Round(106.8, 2, MidpointRounding.AwayFromZero);
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
DarthVegan
  • 1,719
  • 7
  • 25
  • 42
  • If this is for display purposes use `price.ToString('0.00')`. See [Formatting a float to 2 decimal places](http://stackoverflow.com/questions/6356351/formatting-a-float-to-2-decimal-places) – p.s.w.g Nov 19 '14 at 03:38
  • 3
    Simply because numeric types don't store useless trailing/front zeroes. Would be a big waste of space to keep them. Simply pad your numbers when you store them as a string. – Pierre-Luc Pineault Nov 19 '14 at 03:40
  • What you see is the representation of the value. Mathematically, 106.8 and 106.80 (or 106.800000) is exactly the same value and `decimal` type stores value not the representation of it. – tia Nov 19 '14 at 03:48

1 Answers1

4

Floating-point values 106.8d and 106.80d are totally the same. Use .ToString() or string.Format() to represent them as strings with desired formatting.

Diligent Key Presser
  • 4,183
  • 4
  • 26
  • 34