1. 程式人生 > 其它 >字串去除小數點後的0

字串去除小數點後的0

1.方法1

[HttpGet]
        public  string subZeroAndDot(string s)
        {
            string sResult = s.ToString();
            if (sResult.IndexOf(".") < 0)
                return sResult;
            int iIndex = sResult.Length - 1;
            for (int i = sResult.Length - 1; i >= 0; i--)
            {
                
if (sResult.Substring(i, 1) != "0") { iIndex = i; break; } } sResult = sResult.Substring(0, iIndex + 1); if (sResult.EndsWith(".")) sResult = sResult.Substring(0, sResult.Length - 1
); return sResult; }
View Code

2.方法2

[HttpGet]
        public  string subZeroAndDot(string s)
        {
            string sResult = string.Empty;
            if (s.Contains("."))
            {
                sResult = s.TrimEnd('0').TrimEnd('.');//簡寫 sResult = s.TrimEnd('.','0');
            }
            
return sResult; }

3.方法3

public  string subZeroAndDot(string s)
        {
            string sResult = string.Empty;
            decimal ass1 =decimal.parse(s);
            sResult = ass1.tostring("0.###");
            return sResult;
        }