How do you round a number to two decimal places in C#?

后端 未结 15 1255
挽巷
挽巷 2020-11-22 08:59

I want to do this using the Math.Round function

15条回答
  •  余生分开走
    2020-11-22 09:46

    I know its an old question but please note for the following differences between Math round and String format round:

    decimal d1 = (decimal)1.125;
    Math.Round(d1, 2).Dump();   // returns 1.12
    d1.ToString("#.##").Dump(); // returns "1.13"
    
    decimal d2 = (decimal)1.1251;
    Math.Round(d2, 2).Dump();   // returns 1.13
    d2.ToString("#.##").Dump(); // returns "1.13"
    

提交回复
热议问题