类型转成字符串的常用方法

2017-10-14 21:09:13  访问(1463) 赞(0) 踩(0)

using System;
using System.Collections.Generic;
using System.Text;

namespace SlowX.CoreModel.Functions
{
    /// <summary>
    /// 静态常用的方法
    /// </summary>
    public class CmFn
    {
        /// <summary>
        /// 
        /// </summary>
        public CmFn()
        {

        }

        #region 基础函数

        /// <summary>
        /// Int转成字符串
        /// </summary>
        /// <param name="theValue"></param>
        /// <returns></returns>
        public static string IntToString(int theValue)
        {
            return theValue.ToString();
        }

        /// <summary>
        /// UInt转成字符串
        /// </summary>
        /// <param name="theValue"></param>
        /// <returns></returns>
        public static string UIntToString(uint theValue)
        {
            return theValue.ToString();
        }

        /// <summary>
        /// Long转成字符串
        /// </summary>
        /// <param name="theValue"></param>
        /// <returns></returns>
        public static string LongToString(long theValue)
        {
            return theValue.ToString();
        }



        /// <summary>
        /// ULong转成字符串
        /// </summary>
        /// <param name="theValue"></param>
        /// <returns></returns>
        public static string ULongToString(ulong theValue)
        {
            return theValue.ToString();
        }

        /// <summary>
        /// decimal转成字符串
        /// </summary>
        /// <param name="theValue"></param>
        /// <returns></returns>
        public static string DecimalToString(decimal theValue)
        {
            int intValue = (int)theValue;

            if (theValue == intValue)
                return intValue.ToString();

            decimal dValue = Math.Round(theValue, 2);

            if (dValue == theValue)
                return dValue.ToString();

            return theValue.ToString();
        }

        /// <summary>
        /// Float转成字符串
        /// </summary>
        /// <param name="theValue"></param>
        /// <returns></returns>
        public static string FloatToString(float theValue)
        {
            int intValue = (int)theValue;

            if (theValue == intValue)
                return intValue.ToString();

            double dValue = Math.Round((double)theValue, 2);

            if (dValue == theValue)
                return dValue.ToString();

            return theValue.ToString();
        }

        /// <summary>
        /// double转成字符串
        /// </summary>
        /// <param name="theValue"></param>
        /// <returns></returns>
        public static string DoubleToString(double theValue)
        {
            int intValue = (int)theValue;

            if (theValue == intValue)
                return intValue.ToString();

            double dValue = Math.Round(theValue, 2);

            if (dValue == theValue)
                return dValue.ToString();

            return theValue.ToString();
        }

        /// <summary>
        /// Boolean转成字符串
        /// </summary>
        /// <param name="theValue"></param>
        /// <returns></returns>
        public static string BooleanToString(bool theValue)
        {
            if (theValue)
                return "1";
            else
                return "0";

        }

        /// <summary>
        /// DateTime转成yyyy-MM-dd格式的字符串
        /// </summary>
        /// <param name="theValue"></param>
        /// <returns></returns>
        public static string DateTimeToString(DateTime theValue)
        {
            return theValue.ToString("yyyy-MM-dd");
        }

        /// <summary>
        /// DateTime转成yyyy-MM-dd/yyyy-MM-dd HH:mm:ss格式的字符串
        /// </summary>
        /// <param name="theValue"></param>
        /// <param name="isShort"></param>
        /// <returns></returns>
        public static string DateTimeToString(DateTime theValue, bool isShort)
        {
            if (isShort)
                return theValue.ToString("yyyy-MM-dd");
            else
                return theValue.ToString("yyyy-MM-dd HH:mm:ss");
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="theValue"></param>
        /// <returns></returns>
        public static string IntNullToString(int? theValue)
        {
            if (theValue == null)
                return "";

            return theValue.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="theValue"></param>
        /// <returns></returns>
        public static string UIntNullToString(uint? theValue)
        {
            if (theValue == null)
                return "";

            return theValue.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="theValue"></param>
        /// <returns></returns>
        public static string LongNullToString(long? theValue)
        {
            if (theValue == null)
                return "";

            return theValue.ToString();
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="theValue"></param>
        /// <returns></returns>
        public static string ULongNullToString(ulong? theValue)
        {
            if (theValue == null)
                return "";

            return theValue.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="theValue"></param>
        /// <returns></returns>
        public static string DecimalNullToString(decimal? theValue)
        {
            if (theValue == null)
                return "";

            int intValue = (int)theValue;

            if (theValue == intValue)
                return intValue.ToString();

            decimal dValue = Math.Round((decimal)theValue, 2);

            if (dValue == theValue)
                return dValue.ToString();

            return theValue.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="theValue"></param>
        /// <returns></returns>
        public static string FloatNullToString(float? theValue)
        {
            if (theValue == null)
                return "";

            int intValue = (int)theValue;

            if (theValue == intValue)
                return intValue.ToString();

            double dValue = Math.Round((double)theValue, 2);

            if (dValue == theValue)
                return dValue.ToString();

            return theValue.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="theValue"></param>
        /// <returns></returns>
        public static string DoubleNullToString(double? theValue)
        {
            if (theValue == null)
                return "";

            int intValue = (int)theValue;

            if (theValue == intValue)
                return intValue.ToString();

            double dValue = Math.Round((double)theValue, 2);

            if (dValue == theValue)
                return dValue.ToString();

            return theValue.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="theValue"></param>
        /// <returns></returns>
        public static string BooleanNullToString(bool? theValue)
        {
            if (theValue == null)
                return "";

            if (theValue == true)
                return "1";
            else if (theValue == false)
                return "0";
            else
                return "";
        }

        /// <summary>
        /// DateTime?转成字符串(yyyy-MM-dd)格式
        /// </summary>
        /// <param name="theValue"></param>
        /// <returns></returns>
        public static string DateTimeNullToString(DateTime? theValue)
        {
            if (theValue == null)
                return "";

            return theValue.Value.ToString("yyyy-MM-dd");
        }

        /// <summary>
        /// DateTime?转成字符串|isShort:true|(yyyy-MM-dd)格式/false|yyyy-MM-dd HH:mm:ss
        /// </summary>
        /// <param name="theValue"></param>
        /// <param name="isShort"></param>
        /// <returns></returns>
        public static string DateTimeNullToString(DateTime? theValue, bool isShort)
        {
            if (theValue == null)
                return "";

            if (isShort)
                return theValue.Value.ToString("yyyy-MM-dd");
            else
                return theValue.Value.ToString("yyyy-MM-dd HH:mm:ss");
        }

        /// <summary>
        /// bool?转成bool值,null转成defaultValue
        /// </summary>
        /// <param name="theValue"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        protected bool BooleanNullToBoolean(bool? theValue, bool defaultValue)
        {
            if (theValue == null)
                return defaultValue;

            if (theValue == true)
                return true;
            else if (theValue == false)
                return false;
            else
                return defaultValue;
        }

        #endregion 基础函数


    }
}


上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)