How do I round a decimal to 2 decimal places?


  • Hello

    I have this code:

    while (rdr.Read())
    gotPrice = (rdr.GetDecimal(0));
    ....but I can't work out how to properly round the result to 2 decimal places before assigning the variable gotPrice?

    I get this as example: 54.9700

    and I'd like gotPrice = 54.97

    Thanks for your time

    Sunday, October 29, 2006 3:27 PM

All replies

  • Try gotPrice = Math.Round(54.9700,2);

    I hope this helps.

    Sunday, October 29, 2006 3:35 PM
  • To get Decimal back use Math.Round with Second parameter specifying number of decimal points.

    decimal d = 54.9700M;

    decimal f = (Math.Round(d, 2)); // 54.97


    To Get String representation of number use String.Format() Specifiying Fixed Points as F2

    decimal d = 54.9700M;

    string s = String.Format("{0:F2}", d)); // "54.97"

    I hope this will Help!

    Best Regards,



    • Proposed as answer by Sergiu Dudnic Thursday, March 12, 2009 4:18 PM
    Sunday, October 29, 2006 7:55 PM
  • RizwanSharp



    I was testing the sample and it seems that String.Format("{0:F2}",   will round too.  not just trim. 
    would you please review, I could be wrong , just trying to make sure I understand this right


    thank you for your help

    Thursday, October 18, 2007 6:12 PM
  • Ulys,


    You are right, I reproducesd the same what you said. Both ways round the number. Thanks for pointing out.


    Best Regards,

    Rizwan aka RizwanSharp

    Thursday, October 18, 2007 6:25 PM
  • Do you want to round the value to two decimal places or format the value with two decimal plays.  If the later, using a the Decimal.ToString or the String.Format method as described by RizwanSharp.  If you want to round to two decimals use Math.Round, as described by NatasataN.
    Friday, October 19, 2007 4:42 PM