【问题标题】:How do I remove leading zeroes for a negative number [closed]如何删除负数的前导零 [关闭]
【发布时间】:2016-09-07 05:53:49
【问题描述】:

如何在 C# 中删除负数的前导零?

例如,我希望 '-01' 转换为 '-1'。

【问题讨论】:

  • 在 C# 中,' 字符用于包围字符。您需要将" 用于字符串。既然你在谈论数字,你真的需要更清楚地了解字符、字符串或数字。你从什么转换,你转换成什么?投票结束。
  • 如果你指定一个INPUT TYPE和一个OUTPUT TYPE,我们可以构建一个很好的函数,我相信你也可以做到

标签: c# parsing format numbers negative-number


【解决方案1】:
        string InputString = "-020.50";
        for (int i = 0; i < InputString.Length; i++)
        {
            char CurrentCharacter = InputString[i];

            if (Char.IsDigit(CurrentCharacter))
            {
                if (CurrentCharacter == '0')
                {
                    InputString = InputString.Remove(i, 1);
                    i--;
                }
                else
                    break;
            }
        }
        Console.WriteLine(InputString);

【讨论】:

    【解决方案2】:

    假设您的输入将是一个实际的字符串,那么您可以使用任何常见的整数解析方法,如 Int32.Parse()Convert.ToInt32() 应该处理前导零:

    Convert.ToInt32("-01"); // yields -1
    

    幕后花絮

    如果你挖掘into the implementation of these methods,你会看到它调用了一个底层的StringToNumber() 方法和后续的ParseNumber() method,它应该负责处理前导零和尾随零:

    // This is called via the following methods
    // 1.) Convert.ToInt32()
    // 2.) Int32.Parse()
    // 3.) Number.ParseInt32()
    // 4.) StringToNumber() (this method)
    // 5.) ParseNumber() (called by this method)
    [System.Security.SecuritySafeCritical]  // auto-generated
    private unsafe static void StringToNumber(String str, NumberStyles options, ref NumberBuffer number, NumberFormatInfo info, Boolean parseDecimal) {
    
            if (str == null) {
                throw new ArgumentNullException("String");
            }
            Contract.EndContractBlock();
            Contract.Assert(info != null, "");
            fixed (char* stringPointer = str) {
                char * p = stringPointer;
                if (!ParseNumber(ref p, options, ref number, null, info , parseDecimal) 
                    || (p - stringPointer < str.Length && !TrailingZeros(str, (int)(p - stringPointer)))) {
                    throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
                }
            }
    }
    
    private static Boolean TrailingZeros(String s, Int32 index) {
            // For compatability, we need to allow trailing zeros at the end of a number string
            for (int i = index; i < s.Length; i++) {
                if (s[i] != '\0') {
                    return false;
                }
            }
            return true;
    }
    

    【讨论】:

      【解决方案3】:

      int.Parse("-01").ToString() 怎么样?

      【讨论】:

        【解决方案4】:

        你可以尝试解析string:

        var negativeStrNum = "-000005";
        
        Console.WriteLine(int.Parse(negativeStrNum));
        

        这给了你-5

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-21
          • 1970-01-01
          相关资源
          最近更新 更多