数据库sql
1:CONVERT(decimal(18, 2),success * 1.0 / total * 100) percents
2:Convert(numeric(9,2),25*100.0/30)+'%' As 百分比
如果是分组之后 找每个组别占总数百分比 可以 嵌套(求count得sql) 然后运用
select tb.*,CONVERT(decimal(18, 2),TypeName * 1.0 / 总数 * 100) percents from (
select QProom,count(*) TypeNmame from A
group by QProom
)tb
总数可以用 (select count(*) from A) 代替 最后结果
c# 百分比
基本思路: 1 除法 一般都是转 double 然后运算 这样才有小数
2 小数 在转成string 字符串 来格式一下想要的小数
格式化:
ToString("P") //2位小数 ToString("P3") //3位小数
string.Format("{0:0.00%}",percent);//2位小数
string.Format("{0:0.0000%}",percent);//3位小数
函数:
Math.Round(num1,num2) ,参数一(num1)是用于四舍五入的数,参数二(num2)是表示保留小数点后数字的位数
double percent=Math.Round(5*1.00/10*100,2); //50.00
var res=percent.ToString+"%"; //50.00%