如何在c#函数中传递sql的这个百分比计算 [英] How to convet this percentage calculation of sql in c# function

查看:71
本文介绍了如何在c#函数中传递sql的这个百分比计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在c#函数中传递sql的这个百分比计算?



How to convet this percentage calculation of sql in c# function?

select isnull(CAST(ceiling(ROUND(cast(cast(sum(value) AS NUMERIC) / cast(sum(total) AS NUMERIC) * 100 AS DECIMAL(8, 2)), 0)) AS INT), 0) as percentage

推荐答案

var value = 123.45;
var total = 1000.1;
var perc = total == 0 ? 0 : Convert.ToInt32((value * 100) / total);


检查:

Check this:
double avalue = 4.0;
double total = 31.0;

var result = total == 0 ? 0 : Math.Round((avalue/total)*100);
Console.WriteLine("{0}:{1}={2}", avalue, total, result);


和另一个选项...



这个假设该值和总数是整数,你想要一个整数结果

And another option...

This one assumes that value and total are integers and you want an integer result
var value = 4;
var total = 31;
var perc = (total == 0) ? 0 : (int)Math.Ceiling(value * 100.0 / total);


这篇关于如何在c#函数中传递sql的这个百分比计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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