You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#中去除字符串前导零并转换为带点分隔符的小数(示例:将"0000000000002022"转为20.22)

Solution for Removing Leading Zeros and Formatting as Decimal in C#

Here are two reliable approaches to solve your problem, depending on the length of your input string:

Approach 1: Numeric Conversion (Best for Standard Lengths)

This method uses numeric parsing to handle the division, which is great if your input string fits within the range of a decimal (up to 28-29 digits). It’s straightforward and handles validation implicitly (assuming your input is a valid numeric string).

using System.Globalization;

var value = "0000000000002022";

// Remove leading zeros; handle all-zeros case
string trimmedValue = value.TrimStart('0');
if (string.IsNullOrEmpty(trimmedValue))
{
    trimmedValue = "0";
}

// Convert to decimal, divide by 100
decimal numericResult = decimal.Parse(trimmedValue) / 100;

// Format to two decimal places with dot as separator (invariant culture ensures dot)
string finalResult = numericResult.ToString("F2", CultureInfo.InvariantCulture);

Console.WriteLine(finalResult); // Output: 20.22

Key Notes:

  • TrimStart('0') removes all leading zeros efficiently.
  • We check for an empty string after trimming (in case the input was all zeros) to avoid parsing errors.
  • Using decimal ensures precision (critical for financial or numeric calculations).
  • CultureInfo.InvariantCulture guarantees the decimal separator is always a dot, regardless of the system's default culture.

Approach 2: String Manipulation (For Very Long Numeric Strings)

If your input string is longer than what a decimal can handle (more than 29 digits), this method uses string operations to insert the decimal point without converting to a numeric type.

var value = "0000000000002022";

// Remove leading zeros; handle all-zeros case
string trimmedValue = value.TrimStart('0');
if (string.IsNullOrEmpty(trimmedValue))
{
    trimmedValue = "0";
}

string finalResult;
if (trimmedValue.Length <= 2)
{
    // For short strings, pad with leading zeros before the decimal point
    finalResult = $"0.{trimmedValue.PadLeft(2, '0')}";
}
else
{
    // Insert decimal point two positions from the end
    int splitIndex = trimmedValue.Length - 2;
    finalResult = $"{trimmedValue.Substring(0, splitIndex)}.{trimmedValue.Substring(splitIndex)}";
}

Console.WriteLine(finalResult); // Output: 20.22

Key Notes:

  • Works for any length of valid numeric string (no overflow issues).
  • Handles edge cases like "0000" (outputs "0.00") and "01" (outputs "0.01").
  • Uses string interpolation for clean, readable code.

Edge Case Testing

Both approaches handle these scenarios correctly:

  • Input: "0000" → Output: "0.00"
  • Input: "00123" → Output: "1.23"
  • Input: "12300" → Output: "123.00"
  • Input: "0" → Output: "0.00"

内容的提问来源于stack exchange,提问作者DnyaneshSurya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 19:02:40