如何在C#中重新格式化表示小数的字符串,以便有空格组分隔符?

How in C# to reformat string, representing a decimal, to have space group separator?

我在一些文章中发现,在将小数(或其他数字数据类型)转换为字符串的过程中,关于格式化字符串、表示数字、使用数字组分隔符的文章:

如何在C中格式化1700到1700和1000000到1000'000?

C:格式化价格值字符串

如果这些数字已经是字符串格式(通过streamreader从.txt文件中读取),但仍然希望使用组分隔符(和字符串格式),该怎么办?

在这种情况下,将它解析为十进制然后返回到具有所需格式的字符串是最合理的方法吗?


我想其他的。我有另一种格式化字符串值的方法。
您可以检查它:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
''' <summary>
'
'' Parses and formats a string value
''' </summary>
'
'' <param name="value">The value to check and format</param>
''' <param name="f_bAddGroupSep">Specifies the formater whether adds more group separator to formatted value string</param>
'
'' <returns></returns>
''' <remarks></remarks>
Private Function ParseValue(ByVal value As String, _
                            Optional ByVal f_bAddGroupSep As Boolean = True) As String
    '
If invalid number
    Dim f_decValue As Decimal = 0
    If (String.IsNullOrEmpty(value, StringAction.Trim) _
    OrElse (Not (Decimal.TryParse(Trim(value), NumberStyles.Number, Me.Culture, f_decValue)))) Then Return"0"
    'Trims value
    value = Trim(value)
    '
If not negative, remove the negative signal
    If (Not (Me.IsNegative)) Then value = Replace(value, Me.NumberFormat.NegativeSign, vbNullString)
    'Starts formating value
    Dim f_iDecNum As Int32 = 0
    Dim f_decRndValue As Decimal = 0
    Dim f_sNumPat As String = vbNullString
    If (Me.IsDecimal) Then
        '
Parses the decimal digits number
        f_iDecNum = Me.NumberFormat.NumberDecimalDigits
        If (Me.DecimalDigits >= 0) Then f_iDecNum = Me.DecimalDigits
        'If the formatted value had the decimal separator signal
        If (IndexOf(value, Me.NumberFormat.NumberDecimalSeparator) >= 0) Then
            value &= New String("0"c, f_iDecNum)

            '
Else (If the formatted value did not have the decimal separator signal)
        Else
            value &= String.Concat(Me.NumberFormat.NumberDecimalSeparator, New String("0"c, f_iDecNum))
        End If
        'Prepares the number format pattern
        f_sNumPat = String.Concat("N", Convert.ToString(f_iDecNum))

        '
Else
    Else
        'Prepares the number format pattern
        f_sNumPat ="N"
    End If
    Try
        '
Casts the string value to decimal
        f_decValue = Convert.ToDecimal(value, Me.Culture)
        'Prepares the rounded decimal value
        f_decRndValue = f_decValue
        Select Case Me.Round
            '
Rounds down
            Case Util.NumberFormat.RoundDown
                f_decRndValue = Decimal.Round(f_decValue, f_iDecNum)
                If (f_decRndValue <> f_decValue) Then f_decRndValue = Decimal.Round((f_decValue - Convert.ToDecimal(String.Concat("0", Me.NumberFormat.NumberDecimalSeparator, New String("0"c, f_iDecNum),"5"), Me.NumberFormat)), f_iDecNum)
                'Rounds up
            Case Util.NumberFormat.RoundUp
                f_decRndValue = Decimal.Round(f_decValue, f_iDecNum)
                If (f_decRndValue <> f_decValue) Then f_decRndValue = Decimal.Round((f_decValue + Convert.ToDecimal(String.Concat("0", Me.NumberFormat.NumberDecimalSeparator, New String("0"c, f_iDecNum),"5"), Me.NumberFormat)), f_iDecNum)
                '
Rounds by the decimal digits number
            Case Util.NumberFormat.HalfAdjust
                f_decRndValue = Decimal.Round(f_decValue, f_iDecNum)
                'Rounds up
                If (f_decRndValue > f_decValue) Then
                    f_decRndValue = Decimal.Round((f_decValue + Convert.ToDecimal(String.Concat("0", Me.NumberFormat.NumberDecimalSeparator, New String("0"c, f_iDecNum),"5"), Me.NumberFormat)), f_iDecNum)

                    '
Else (Rounds down)
                ElseIf (f_decRndValue < f_decValue) Then
                    f_decRndValue = Decimal.Round((f_decValue - Convert.ToDecimal(String.Concat("0", Me.NumberFormat.NumberDecimalSeparator, New String("0"c, f_iDecNum),"5"), Me.NumberFormat)), f_iDecNum)
                End If
        End Select
        'Returns the formatted decimal value
        value = f_decRndValue.ToString(f_sNumPat, Me.Culture)
    Catch ex As Exception
        f_decValue = 0
        f_decRndValue = 0
        value ="0"
    End Try
    Return value
End Function
  • 使用属性:
    ----------------
  • me.isdecimal:指定是否将字符串值格式化为十进制数
  • me.isnegative:指定是否接受负十进制值
  • me.culture:您的文化信息
  • me.numberFormat:数字格式信息。它来自Me.Culture(=Me.Culture.NumberFormat)
  • Me.Round:舍入小数的方法(向上舍入、向下舍入或半调整(按十进制数字舍入))
  • me.decimal digits:小数位数

出于好奇,我把这个方法放在一起,看看在不解析和格式化的情况下向字符串添加分隔符需要什么:

1
2
3
4
5
6
7
8
9
10
11
12
public static string AddGroupSeparators(string number) {
  int[] sizes = CultureInfo.CurrentCulture.NumberFormat.NumberGroupSizes;
  int pos = number.LastIndexOf(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
  if (pos == -1) pos = number.Length;
  int sizeIndex = 0;
  while (sizes[sizeIndex] > 0 && pos > sizes[sizeIndex]) {
    pos -= sizes[sizeIndex];
    number = number.Insert(pos, CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator);
    if (sizeIndex < sizes.Length - 1) sizeIndex++;
  }
  return number;
}


Is parsing it to decimal and then back
to string with needed formatting the
most reasonable way

对。这将比使用字符串操作更简单。