Files
TaskFlow/app/TaskFlow/ClassFormat.vb

169 lines
6.7 KiB
VB.net

Imports System.Globalization
Imports DevExpress.CodeParser
Imports DevExpress.Data.Controls
Imports DevExpress.XtraPrinting
Public Class ClassFormat
Public Const CURRENCY_FORMAT = "C2"
Public Const DECIMAL_FORMAT = "F"
Public Const DATE_FORMAT = "d"
Public Const DATETIME_FORMAT = "G"
Public Shared Function GetFormatString(pFormatString As String) As String
Select Case pFormatString
Case ClassControlCreator.CONTROL_TYPE_CURRENCY
Return CURRENCY_FORMAT
Case ClassControlCreator.CONTROL_TYPE_DOUBLE
Return DECIMAL_FORMAT
Case ClassControlCreator.CONTROL_TYPE_DATE
Return DATE_FORMAT
Case ClassControlCreator.CONTROL_TYPE_DATETIME
Return DATETIME_FORMAT
Case Else
Return String.Empty
End Select
End Function
''' <summary>
''' Normalisiert einen numerischen String für die invariante Kultur-Konvertierung.
''' Entfernt Tausendertrennzeichen und ersetzt Dezimaltrennzeichen durch Punkt.
''' </summary>
Private Shared Function NormalizeNumericString(pValue As String) As String
If String.IsNullOrWhiteSpace(pValue) Then
Return pValue
End If
Dim normalized As String = pValue.Trim()
' Entferne Währungssymbole und Leerzeichen
normalized = System.Text.RegularExpressions.Regex.Replace(normalized, "[€$£¥\s]", "")
' Prüfe, ob der String sowohl Punkt als auch Komma enthält
Dim hasDot As Boolean = normalized.Contains(".")
Dim hasComma As Boolean = normalized.Contains(",")
If hasDot AndAlso hasComma Then
' Beide vorhanden: Das letzte ist der Dezimaltrenner
Dim lastDotPos As Integer = normalized.LastIndexOf(".")
Dim lastCommaPos As Integer = normalized.LastIndexOf(",")
If lastDotPos > lastCommaPos Then
' Punkt ist Dezimaltrenner, Komma ist Tausendertrenner
normalized = normalized.Replace(",", "")
Else
' Komma ist Dezimaltrenner, Punkt ist Tausendertrenner
normalized = normalized.Replace(".", "").Replace(",", ".")
End If
ElseIf hasComma Then
' Nur Komma: Könnte Dezimal- oder Tausendertrenner sein
' Wenn mehr als ein Komma → Tausendertrenner
' Wenn nur ein Komma und <= 3 Stellen danach → Dezimaltrenner
Dim commaCount As Integer = normalized.Count(Function(c) c = ","c)
If commaCount = 1 Then
Dim lastCommaPos As Integer = normalized.LastIndexOf(",")
Dim digitsAfterComma As Integer = normalized.Length - lastCommaPos - 1
If digitsAfterComma <= 3 Then
' Wahrscheinlich Dezimaltrenner
normalized = normalized.Replace(",", ".")
Else
' Wahrscheinlich Tausendertrenner
normalized = normalized.Replace(",", "")
End If
Else
' Mehrere Kommas → Tausendertrenner
normalized = normalized.Replace(",", "")
End If
End If
' Wenn nur Punkt vorhanden: bereits im richtigen Format
Return normalized
End Function
''' <summary>
''' Converts a string according to the type information, using the invariant culture
''' </summary>
''' <param name="pValue"></param>
''' <param name="pType"></param>
''' <returns></returns>
Public Shared Function GetConvertedValue(pValue As Object, pType As String) As Object
Dim oConvertedValue
LOGGER.Debug($"GetConvertedValue: {pType}")
Select Case pType
Case ClassControlCreator.CONTROL_TYPE_DOUBLE
Try
Dim normalizedValue As String = NormalizeNumericString(pValue?.ToString())
If Double.TryParse(normalizedValue, NumberStyles.Float, CultureInfo.InvariantCulture, oConvertedValue) Then
Return oConvertedValue
End If
Catch ex As Exception
LOGGER.Error(ex)
End Try
Case ClassControlCreator.CONTROL_TYPE_CURRENCY
Try
Dim normalizedValue As String = NormalizeNumericString(pValue?.ToString())
LOGGER.Debug($"GetConvertedValue: Converting {pValue.ToString} (normalized: {normalizedValue}) to Currency ")
If Double.TryParse(normalizedValue, NumberStyles.Float, CultureInfo.InvariantCulture, oConvertedValue) Then
Return oConvertedValue
End If
Catch ex As Exception
LOGGER.Error(ex)
End Try
Case ClassControlCreator.CONTROL_TYPE_INTEGER
Try
Dim normalizedValue As String = NormalizeNumericString(pValue?.ToString())
If Integer.TryParse(normalizedValue, NumberStyles.Integer, CultureInfo.InvariantCulture, oConvertedValue) Then
Return oConvertedValue
End If
Catch ex As Exception
LOGGER.Error(ex)
End Try
Case Else
LOGGER.Debug($"GetConvertedValue - Case ELSE - pType is {pType}")
Try
oConvertedValue = pValue.ToString
Catch ex As Exception
LOGGER.Warn($"Error in GetConvertedValue: pType is {pType} - converting value to String")
oConvertedValue = ""
End Try
End Select
Return oConvertedValue
End Function
''' <summary>
''' Converts values to their respective data type and then back to string
''' according to the current culture
''' </summary>
''' <param name="pValue"></param>
''' <returns></returns>
Public Shared Function GetStringValue(pValue As Object) As String
Select Case pValue.GetType
Case GetType(Single)
Return DirectCast(pValue, Single).ToString(CultureInfo.CurrentCulture)
Case GetType(Double)
Return DirectCast(pValue, Double).ToString(CultureInfo.CurrentCulture)
Case GetType(Decimal)
Return DirectCast(pValue, Decimal).ToString(CultureInfo.CurrentCulture)
Case GetType(Date)
Return DirectCast(pValue, Date).ToString(CultureInfo.CurrentCulture)
Case GetType(DateTime)
Return DirectCast(pValue, DateTime).ToString(CultureInfo.CurrentCulture)
Case Else
Return pValue.ToString
End Select
End Function
End Class