Someone has any idea how can I auto round double values to 2 decimal digits? I have an application which performs some sim开发者_开发问答ple calculations in many places and sometime I'm getting numbers with a lot of decimal numbers such as 21.248123. Is there any way to specify .NET to use only 2 decimal number in every place (through CultureInfo maybe?).
Thanks
You can do this at least when displaying the message to the user by using myDouble.ToString("0.00")
.
If you want to round the value itself use Math.Round(myDouble,2)
.
Note that even when rounding the double
, you might get many decimals due to floating point aritmetics, so I would use the former ToString
variant regardless when displaying the value to the user to make sure that it gets displayed with only two decimals.
精彩评论