How to remove decimal part from a number in C#

后端 未结 7 826
予麋鹿
予麋鹿 2020-12-08 04:12

I have number of type double. double a = 12.00 I have to make it as 12 by removing .00

Please help me

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-08 04:33

    Reading all the comments by you, I think you are just trying to display it in a certain format rather than changing the value / casting it to int.

    I think the easiest way to display 12.00 as "12" would be using string format specifiers.

    double val = 12.00;
    
    string displayed_value = val.ToString("N0"); // Output will be "12"
    

    The best part about this solution is, that it will change 1200.00 to "1,200" (add a comma to it) which is very useful to display amount/money/price of something.

    More information can be found here: https://msdn.microsoft.com/en-us/library/kfsatb94(v=vs.110).aspx

提交回复
热议问题