Created
July 30, 2014 10:18
-
-
Save jrgcubano/81d03e05aeb36054c6c4 to your computer and use it in GitHub Desktop.
Numeric Converters from string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Windows.Data; | |
using System.Globalization; | |
namespace PMS_UI.CommonControls.Converters | |
{ | |
public class NumericConverter : IValueConverter | |
{ | |
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) | |
{ | |
if (null == value) | |
return null; | |
else | |
if (targetType.UnderlyingSystemType == typeof(String)) | |
return value.ToString(); | |
else | |
return value; | |
} | |
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) | |
{ | |
//Create numbers from formatted text. | |
if (null == value) | |
{ return null; } | |
else | |
{ | |
HashSet<Type> numericTypes = new HashSet<Type> | |
{typeof(Byte), | |
typeof(Decimal), | |
typeof(Double), | |
typeof(Int16), | |
typeof(Int32), | |
typeof(Int64), | |
typeof(SByte), | |
typeof(Single), | |
typeof(UInt16), | |
typeof(UInt32), | |
typeof(UInt64) | |
}; | |
if (numericTypes.Contains(targetType.UnderlyingSystemType)) | |
{ | |
HashSet<Type> intTypes = new HashSet<Type> | |
{typeof(Byte), | |
typeof(Int16), | |
typeof(Int32), | |
typeof(Int64), | |
typeof(SByte), | |
typeof(UInt16), | |
typeof(UInt32), | |
typeof(UInt64) | |
}; | |
string text = value.ToString().Replace(NumberFormatInfo.CurrentInfo.NumberGroupSeparator, String.Empty); | |
if (intTypes.Contains(targetType.UnderlyingSystemType)) | |
{ | |
try { return Int32.Parse(text); } | |
catch { return null; } | |
} | |
else | |
{ | |
try { return Double.Parse(text); } | |
catch { return null; } | |
} | |
} | |
else { return value; } | |
} | |
} | |
} | |
public class NumericConverterOneWay : IValueConverter | |
{ | |
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) | |
{ | |
if (null == value) | |
return null; | |
else | |
if (targetType.UnderlyingSystemType == typeof(String)) | |
return value.ToString(); | |
else | |
return value; | |
} | |
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) | |
{ | |
//Create numbers from formatted text. | |
return value; | |
//return ConverterHelper.GetValueFromString(value, targetType); | |
} | |
} | |
public class NumericConverterWithType : IValueConverter | |
{ | |
public Type TargetType { get; set; } | |
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) | |
{ | |
if (null == value) | |
return null; | |
else | |
if (targetType.UnderlyingSystemType == typeof(String)) | |
return value.ToString(); | |
else | |
return value; | |
} | |
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) | |
{ | |
// Create numbers from formatted text. | |
return ConverterHelper.GetValueFromString(value, TargetType); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment