Home  >  Article  >  Backend Development  >  C# Format string type amount

C# Format string type amount

黄舟
黄舟Original
2017-02-20 10:58:262093browse

C# Format string type amount

   /// <summary>
        /// 根据精度将金额转换为自定义类型的string
        /// </summary>
        /// <param name="precision">需要的精度</param>
        /// <param name="money">需要转换的金额</param>
        /// <returns>转换后的金额</returns>
        public  string FormateString(int precision, double money)
        {
            //比如,FormateString(2,2333)==>结果:2,333.00
            string format = "###,###,###,##0.";
            for (int i = 0; i < precision; i++)
            {
                format = format + "0";
            }
            return money.ToString(format);
        }
        /// <summary>
        /// 转换任意类型的对象为Double
        /// </summary>
        /// <param name="val">需要转换的对象</param>
        /// <returns>返回Double类型的变量</returns>
        public  double ConvertDouble(object val)
        {
            if (((val == null) || (val.ToString() == "")) || (val is DBNull))
            {
                return 0.0;
            }
            if (val is string)
            {
                val = val.ToString().Replace(",", "");
            }
            try
            {
                return Convert.ToDouble(val);
            }
            catch
            {
                return 0.0;
            }
        }

Small note:

Custom format display: click to open the link

The above is the content of C# Formatting string type amount. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn