Dreamine.UI.Wpf 1.0.1
Dreamine.UI.Wpf 사용자 인터페이스 기능과 구성 요소를 제공합니다.
로딩중...
검색중...
일치하는것 없음
StringToDoubleConverter.cs
이 파일의 문서화 페이지로 가기
1using System.Globalization;
2using System.Windows.Data;
3
5{
14 public sealed class StringToDoubleConverter : IValueConverter
15 {
24 public double Fallback { get; set; } = 0.0;
25
34 public string? Format { get; set; }
35
44 public bool UseInvariantCulture { get; set; } = true;
45
94 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
95 {
96 var ci = UseInvariantCulture ? CultureInfo.InvariantCulture : (culture ?? CultureInfo.CurrentCulture);
97
98 if (value is string s)
99 {
100 if (double.TryParse(s, NumberStyles.Float | NumberStyles.AllowLeadingSign, ci, out var d))
101 return d;
102
103 // Common culture confusion fix: replace ',' with '.' if using invariant culture
104 if (UseInvariantCulture && double.TryParse(s.Replace(',', '.'), NumberStyles.Float | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out d))
105 return d;
106 }
107 else if (value is IConvertible c)
108 {
109 try { return System.Convert.ToDouble(c, ci); }
110 catch { /* ignored */ }
111 }
112
113 return Fallback;
114 }
115
172 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
173 {
174 var ci = UseInvariantCulture ? CultureInfo.InvariantCulture : (culture ?? CultureInfo.CurrentCulture);
175
176 double d = value switch
177 {
178 double v => v,
179 float v => v,
180 decimal v => (double)v,
181 string s when double.TryParse(s, NumberStyles.Float | NumberStyles.AllowLeadingSign, ci, out var vv) => vv,
182 _ => Fallback
183 };
184
185 var fmt = string.IsNullOrWhiteSpace(Format) ? "G" : Format!;
186 return d.ToString(fmt, ci);
187 }
188 }
189}
object Convert(object value, Type targetType, object parameter, CultureInfo culture)
object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)