zoukankan      html  css  js  c++  java
  • 数字转化成 比例图片 适合 投票,百分率显示

    /// 显示图象
     /// </summary>
     /// <param name="strContent">相关主题下的内容</param>
     /// <param name="decNumAll">所有的票数</param>
     /// <returns></returns>
        public string GetResult(string strContent, decimal decNumAll)
        {
            string[] arrContent = strContent.Split('|');
            string strBody = "<table width=100% border=1>\n";
            foreach (string strContentIN in arrContent)
            {
                string strItemName = strContentIN.Split(',')[1].ToString();//得到选项名称
                decimal decItemNum = Convert.ToDecimal(strContentIN.Split(',')[0]);//得到选项的投票数
                decimal decPercent = GetPercent(decItemNum, decNumAll) * 100;//得到百分比    
                string strPercent = decPercent.ToString();//将百分比转为字符型
                if (strPercent.Length > 5)//如果百分比结果长度超过5位
                {
                    strPercent = strPercent.Substring(0, 5);//将百分比的余数截短为“00.00”
                }
                strBody += "<tr><td width=100>" + strItemName + "</td><td width=50>" + decItemNum.ToString() + "票</td><td><img src=Images/bar1.gif height=10 width=" + strPercent + "%>" + strPercent + "%</td></tr>\n";
            }
            strBody += "</table>";
            return strBody;
        }
     

     /// <summary>
     /// 票数求和
     /// </summary>
     /// <param name="strNum">当前所要操作的字串</param>
     /// <returns></returns>
        public decimal GetNumAll(string strNum)
        {
            decimal decNumAll = 0;
            string[] arrNum = strNum.Split('|');
            foreach (string strNumIN in arrNum)
            {
                decNumAll += Convert.ToInt32(strNumIN.Split(',')[0].ToString());//截取第零位
            }
            return decNumAll;
        }
     
     /// <summary>
     /// 百分比
     /// </summary>
     /// <param name="decItem">当前选项本身的票数</param>
     /// <param name="decNumAll">所有的票数</param>
     /// <returns></returns>
        public decimal GetPercent(decimal decItem, decimal decNumAll)
        {
            if (decNumAll == 0)//如果总票数是零
            {
                decNumAll++;//加一,避免除0出错
            }
            decimal decPercent = decItem / decNumAll;
            return decPercent;
        }

  • 相关阅读:
    1. 二分查找
    Filezilla使用
    正则表达式regex
    TCP的三次握手和四次挥手
    Pycharm 更换源
    寒假学习进度(十四)
    寒假学习进度(十)
    寒假学习进度(九)
    寒假学习进度(八)
    寒假学习进度(七)
  • 原文地址:https://www.cnblogs.com/xianzuoqiaoqi/p/1519384.html
Copyright © 2011-2022 走看看