Я думаю, что другие. У меня есть другой способ отформатировать строковое значение.
Вы можете проверить это:
''' <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.DecimalDigits: количество десятичных цифр