private static void AssertCanConvertEnglishNumbers(ConverterBase decimalConverter) { Assert.AreEqual(123.12, decimalConverter.StringToField("123.12"), "If no culture is specified, the decimal separator should be a dot"); Assert.AreEqual(1234.12, decimalConverter.StringToField("1,234.12"), "If no culture is specified, the group separator should be a comma"); }
public static object StringToField(this IFieldInfoDescriptor fieldDescriptor, string value) { if (value == null) { return(null); } value = fieldDescriptor.StringTrim(value); if (string.Empty.Equals(value) && fieldDescriptor.Converter == null) { return(value); } if (fieldDescriptor.Converter == null && fieldDescriptor.Type == null) { return(value); } ConverterBase converterInstance = fieldDescriptor.Converter == null ? ConverterFactory.GetDefaultConverter(fieldDescriptor.Type, fieldDescriptor.ConverterFormat) : ConverterFactory.GetConverter(fieldDescriptor.Converter, fieldDescriptor.ConverterFormat); return(converterInstance == null ? value : converterInstance.StringToField(value)); }
internal object AssignFromString(ExtractedInfo fieldString, LineInfo line) { object val; switch (mTrimMode) { case TrimMode.None: break; case TrimMode.Both: fieldString.TrimBoth(mTrimChars); break; case TrimMode.Left: fieldString.TrimStart(mTrimChars); break; case TrimMode.Right: fieldString.TrimEnd(mTrimChars); break; } try { if (mConvertProvider == null) { if (mIsStringField) { val = fieldString.ExtractedString(); } else { // Trim it to use Convert.ChangeType fieldString.TrimBoth(WhitespaceChars); if (fieldString.Length == 0) { // Empty stand for null val = GetNullValue(); } else { val = Convert.ChangeType(fieldString.ExtractedString(), mFieldType, null); } } } else { if (mConvertProvider.CustomNullHandling == false && fieldString.HasOnlyThisChars(WhitespaceChars)) { val = GetNullValue(); } else { var from = fieldString.ExtractedString(); val = mConvertProvider.StringToField(from); if (val == null) { val = GetNullValue(); } } } return(val); } catch (ConvertException ex) { var e = ConvertException.ReThrowException(ex, mFieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1); throw e; } }
private static void AssertCanConvertFrenchNumbers(ConverterBase decimalConverterWithoutCulture) { Assert.AreEqual(1.23, decimalConverterWithoutCulture.StringToField("1,23"), "If a culture is specified, the decimal separator should be the specified culture decimal separator"); Assert.AreEqual(1234.12, decimalConverterWithoutCulture.StringToField("1 234,12"), "If a culture is specified, the group separator should be the specified culture group separator"); Assert.Catch(() => { decimalConverterWithoutCulture.StringToField("1.23"); }, "The dot is not a valid french separator"); }