想要将小数点后的值舍入为2位数 [英] want to round the values to 2 digit after the decimal points

查看:97
本文介绍了想要将小数点后的值舍入为2位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果值为0.0009785意味着,我想得到像这样的值0.98

解决方案

我相信你并不需要舍入任何数值。这很少需要,很少。谁想要在计算中失去精确度?最有可能的是,你只想以字符串的形式呈现一个数字,以在屏幕上或其他地方显示价值。



这称为格式化。尽管此类操作在内部也基于舍入,但您永远不会对代码中的任何数值进行舍入,因此可以避免意外丢失准确性的风险。要将数值格式化为字符串,您只需使用其中一个 ToString 方法,并使用参数设置格式,精度,文化和/或其他格式详细信息。例如,对于 double ,请参阅:

http://msdn.microsoft.com/en-us/library/system.double.tostring.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/0c899ak8.aspx [ ^ ]。



-SA


注意 Math.Round(Double,Integer) function 。


double m = Math.Round(0.5500000 0000000004,2);

If the value is 0.0009785 means,i want to get the value like this 0.98

解决方案

I believe you don't really need to round any numeric values. This is rarely needed, very rarely. Who wants to loose precision in calculations? Most likely, you only want to present a number in a form of string, to show the value on screen or somewhere else.

This is called "formatting". Even though such operation is internally also based on rounding, you never round any numeric values in your code, so you avoid a risk of accidental loss of accuracy. And to format the numerical values to string, you just need to use one of its ToString methods with parameters setting format, precision, culture and/or other formatting detail. For example, for double, please see:
http://msdn.microsoft.com/en-us/library/system.double.tostring.aspx[^],
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^].

—SA


Watch for Math.Round(Double, Integer) function.


double m = Math.Round(0.55000000000000004,2);


这篇关于想要将小数点后的值舍入为2位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆