C#中得到两个数百分比 (转)
2013-01-31 16:25
281 查看
//此方法得到的百分比后小数太多,不行
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#:通过MD5得到文件和String的校验值
- C#得到硬盘序列号、网卡序列号
- 【转】C#得到局域网内所有主机名,IP地址,MAC地址
- 怎样用C#得到当前时间
- 利用.NET框架提供的 WebClient类 和 WebRequest类,我们可以很轻易地得到给定URL地址的源代码,很简单,以下是C#的完整的例子.
- 在C#中如何得到主机名称和ip地址
- C#中得到程序当前工作目录和执行目录的一些方法
- C#面向对象编程-计算两个数的加减乘除
- C#中得到可执行文件(程序)的路径和文件名
- c#获取应用程序目录,C#中得到程序当前工作目录和执行目录方法大全
- 总结C#中得到程序当前工作目录和执行目录的一些方法
- 轻松得到适用于C# ADO.NET的各种数据库连接字符串
- C#得到系统进程和结束某个指定的进程
- 复习C#得到的启示
- asp.net(c#)中,两个整数相除,怎样得到带小数点的结果呢?
- C#中得到可执行文件(程序)的路径和文件名