C# Format string type amount

黄舟
Release: 2017-02-20 10:58:26
Original
2135 people have browsed it

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;
            }
        }
Copy after login

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)!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template