C#中得到两个数百分比 (转)
2016-11-09 10:04
411 查看
//此方法得到的百分比后小数太多,不行
double percent=Convert.ToDouble(2)/Convert.ToDouble(34);
string result=(percent*100).ToString()+"%";//得到的是5.8823529411764%
//此方法能得到你想要的小数点后位数
double percent=Convert.ToDouble(2)/Convert.ToDouble(34);
string result=string.Format("{0:0.00%}",percent);//得到5.88%
string result=string.Format("{0:0.0000%}",percent);//得到5.8824%
//用这个方法比较理想,能得到你想要的小数点后位数
double percent=Convert.ToDouble(2)/Convert.ToDouble(34);
string result=percent.ToString("0%");//得到6%
string result=percent.ToString("0.000%");//得到5.882%
//此方法ToString("p")中的参数p默认保留小数点后两位数,这个也能得到你理想的数字
double percent=Convert.ToDouble(2)/Convert.ToDouble(34);
string result=percent.ToString("p");//可以到的5.88%
string result =percent.ToString("p2");//得到5.88%
string result=percent.ToString("p3");//得到5.882%
//Math.Round(num1,num2)方法,参数一(num1)是用于四舍五入的数,参数二(num2)是表示保留小数点后数字的位数
double percent=Math.Round(2*1.00/34*100.0,4);
string result=percent.ToString()+"%";//得到5.8824%
转自 http://blog.sina.com.cn/s/blog_661beca00100subx.html
double percent=Convert.ToDouble(2)/Convert.ToDouble(34);
string result=(percent*100).ToString()+"%";//得到的是5.8823529411764%
//此方法能得到你想要的小数点后位数
double percent=Convert.ToDouble(2)/Convert.ToDouble(34);
string result=string.Format("{0:0.00%}",percent);//得到5.88%
string result=string.Format("{0:0.0000%}",percent);//得到5.8824%
//用这个方法比较理想,能得到你想要的小数点后位数
double percent=Convert.ToDouble(2)/Convert.ToDouble(34);
string result=percent.ToString("0%");//得到6%
string result=percent.ToString("0.000%");//得到5.882%
//此方法ToString("p")中的参数p默认保留小数点后两位数,这个也能得到你理想的数字
double percent=Convert.ToDouble(2)/Convert.ToDouble(34);
string result=percent.ToString("p");//可以到的5.88%
string result =percent.ToString("p2");//得到5.88%
string result=percent.ToString("p3");//得到5.882%
//Math.Round(num1,num2)方法,参数一(num1)是用于四舍五入的数,参数二(num2)是表示保留小数点后数字的位数
double percent=Math.Round(2*1.00/34*100.0,4);
string result=percent.ToString()+"%";//得到5.8824%
转自 http://blog.sina.com.cn/s/blog_661beca00100subx.html
相关文章推荐
- C#中得到两个数百分比
- C#中得到两个数百分比 (转)
- C#得到两个数的百分比
- 1. 请用C#的面向对象语言 实现 一个计算器控制台程序,要求输入两个数和运算符号,得到结果
- [C#应用]得到操作系统名称
- 如何在c#中得到当前目录的路径
- 在C#中用SQLDMO得到指定SQL服务器所有数据库的列表
- 利用.NET框架提供的 WebClient类 和 WebRequest类,我们可以很轻易地得到给定URL地址的源代码,很简单,以下是C#的完整的例子.
- 根据区位得到汉字拼音首字母(c#)
- 探讨?C#中如何得到双字节字符的ASCII码
- C#得到MAC,IP,主机名 [原]
- 用C#通过网址得到域名
- 利用c#的反射机制得到类的信息
- C#: 得到系统中的环境变量(源代码)
- 如何用C#及JS得到服务器端和客户端的IP
- [C#应用]得到组件事件的委托列表
- 如何在c#中得到当前目录的路径
- 用C#得到Oracle配置的连接串
- 用C#通过网址得到域名
- 在C#中怎么得到汉字的拼音码