50

I am interested in how to round variables to two decimal places. In the example below, the bonus is usually a number with four decimal places. Is there any way to ensure the pay variable is always rounded to two decimal places?

  pay = 200 + bonus;
  Console.WriteLine(pay);
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Kerry G
  • 917
  • 4
  • 12
  • 21

8 Answers8

100

Use Math.Round and specify the number of decimal places.

Math.Round(pay,2);

Math.Round Method (Double, Int32)

Rounds a double-precision floating-point value to a specified number of fractional digits.

Or Math.Round Method (Decimal, Int32)

Rounds a decimal value to a specified number of fractional digits.

Habib
  • 219,104
  • 29
  • 407
  • 436
36

You should use a form of Math.Round. Be aware that Math.Round defaults to banker's rounding (rounding to the nearest even number) unless you specify a MidpointRounding value. If you don't want to use banker's rounding, you should use Math.Round(decimal d, int decimals, MidpointRounding mode), like so:

Math.Round(pay, 2, MidpointRounding.AwayFromZero); // .005 rounds up to 0.01
Math.Round(pay, 2, MidpointRounding.ToEven);       // .005 rounds to nearest even (0.00) 
Math.Round(pay, 2);    // Defaults to MidpointRounding.ToEven

(Why does .NET use banker's rounding?)

Community
  • 1
  • 1
Jon Senchyna
  • 7,867
  • 2
  • 26
  • 46
6
decimal pay  = 1.994444M;

Math.Round(pay , 2); 
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
5

You can round the result and use string.Format to set the precision like this:

decimal pay = 200.5555m;
pay = Math.Round(pay + bonus, 2);
string payAsString = string.Format("{0:0.00}", pay);
Paul Aldred-Bann
  • 5,840
  • 4
  • 36
  • 55
  • 1
    I doubt you can use `pay` in two different senses in the first line! When you already use `Math.Round`, maybe it's not nexessary to use string formatting as well? If you don't want to round the actual number struct, but only want to shorten the string produced, string formatting is a nice thing. In that case, also consider `"F"` or `"F2"` (and `"N"`, `"N2"`) formatting strings, i.e. for example `string.Format("{0:N}", pay)` or equivalently `pay.ToString("N")`. – Jeppe Stig Nielsen Sep 27 '12 at 13:05
  • @JeppeStigNielsen You're absolutely right about the first line! Amended. Interesting, I'd never used `{0:N}` before, creature of habit I suppose ;o) – Paul Aldred-Bann Sep 27 '12 at 13:12
  • To explain further, `"F"` rounds to a number of decimals specified by the `NumberFormat` of the current culture of the current thread. This would often be two decimals. `"F2"` always rounds to two decimals. `"N"` and `"N2"` are similar but they also insert group separators according to the culture. For example `(11223344.5566m).ToString("N")` might produce the formatted number `"11,223,344.56"` in some culture. See [Standard Numeric Format Strings](http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx). – Jeppe Stig Nielsen Sep 27 '12 at 13:30
2

Use System.Math.Round to rounds a decimal value to a specified number of fractional digits.

var pay = 200 + bonus;
pay = System.Math.Round(pay, 2);
Console.WriteLine(pay);

MSDN References:

Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
2

Make sure you provide a number, typically a double is used. Math.Round can take 1-3 arguments, the first argument is the variable you wish to round, the second is the number of decimal places and the third is the type of rounding.

double pay = 200 + bonus;
double pay = Math.Round(pay);
// Rounds to nearest even number, rounding 0.5 will round "down" to zero because zero is even
double pay = Math.Round(pay, 2, MidpointRounding.ToEven);
// Rounds up to nearest number
double pay = Math.Round(pay, 2, MidpointRounding.AwayFromZero);
Sean
  • 747
  • 4
  • 8
2

Pay attention on fact that Round rounds.

So (I don't know if it matters in your industry or not), but:

float a = 12.345f;
Math.Round(a,2);

//result:12,35, and NOT 12.34 !

To make it more precise for your case we can do something like this:

int aInt = (int)(a*100);
float aFloat= aInt /100.0f;
//result:12,34 
Tigran
  • 61,654
  • 8
  • 86
  • 123
1
Console.WriteLine(decimal.Round(pay,2));