/// <summary> /// Convert the three-digit ISO-4217 currency code to the equivalent <see cref="CurrencyInfo"/>, a return value indicating whether it was successful. /// </summary> /// <param name="numeric">The numeric.</param> /// <param name="result">The result.</param> /// <returns></returns> public static bool TryFromNumeric(string numeric, out CurrencyInfo result) { result = default; if (string.IsNullOrWhiteSpace(numeric)) { return(false); } numeric = numeric.Trim(); if (numeric.Length != CodeLength) { return(false); } result = currencies.SingleOrDefault(item => string.Equals(item.Numeric, numeric, StringComparison.OrdinalIgnoreCase)); return(!string.IsNullOrWhiteSpace(result.Code)); }
/// <summary> /// Convert the three-letter ISO-4217 currency code to the equivalent <see cref="CurrencyInfo"/>, a return value indicating whether it was successful. /// </summary> /// <param name="code">The code.</param> /// <param name="result">The result.</param> /// <returns></returns> public static bool TryFromCode(string code, out CurrencyInfo result) { result = default; if (string.IsNullOrWhiteSpace(code)) { return(false); } code = code.Trim(); if (code.Length != CodeLength) { return(false); } result = currencies.SingleOrDefault(item => string.Equals(item.Code, code, StringComparison.OrdinalIgnoreCase)); return(!string.IsNullOrWhiteSpace(result.Code)); }
/// <summary> /// Converts the string representation of a number to its <see cref="Money" /> equivalent. A return value indicates whether the conversion succeeded or failed. /// </summary> /// <param name="value">The value.</param> /// <param name="result">The result.</param> /// <returns></returns> public static bool TryParse(string value, out Money result) { result = default; if (string.IsNullOrWhiteSpace(value)) { return(false); } value = value.Trim(); if (string.IsNullOrEmpty(value)) { return(false); } if (char.IsNumber(value[0])) { if (decimal.TryParse(value, out var tmp)) { result = new Money(CurrencyInfo.CurrentCurrency, tmp); return(true); } return(false); } if (value.Length < 3) { return(false); } if (CurrencyInfo.TryFromCode(value.Substring(0, 3), out var currency)) { if (value.Length == 3) { result = new Money(currency, decimal.Zero); } else { if (decimal.TryParse(value.Substring(3).Trim().TrimStart(currency.Symbol.ToCharArray()), out var tmp)) { result = new Money(currency, tmp); } else { return(false); } } return(true); } return(false); }
public static bool TryFromCulture(CultureInfo culture, out CurrencyInfo result) { result = default; if (culture == null) { return(false); } if (culture.IsNeutralCulture) { return(false); } if (string.IsNullOrWhiteSpace(culture.Name)) { return(false); } try { return(TryFromRegion(new RegionInfo(culture.Name), out result)); } catch { return(false); } }
private static IFormatProvider GetFormatProvider(CurrencyInfo currency, IFormatProvider provider, bool useCode = false) { NumberFormatInfo numberFormatInfo = null; if (provider != null) { if (provider is CultureInfo culture) { numberFormatInfo = culture.NumberFormat.Clone() as NumberFormatInfo; } if (provider is NumberFormatInfo format) { numberFormatInfo = format.Clone() as NumberFormatInfo; } } else { numberFormatInfo = CultureInfo.CurrentCulture.NumberFormat.Clone() as NumberFormatInfo; } if (numberFormatInfo == null) { return(null); } numberFormatInfo.CurrencyDecimalDigits = (int)currency.DecimalDigits; numberFormatInfo.CurrencySymbol = currency.Symbol; if (useCode) { numberFormatInfo.CurrencySymbol = currency.Code; if (numberFormatInfo.CurrencyPositivePattern == 0) { numberFormatInfo.CurrencyPositivePattern = 2; } if (numberFormatInfo.CurrencyPositivePattern == 1) { numberFormatInfo.CurrencyPositivePattern = 3; } switch (numberFormatInfo.CurrencyNegativePattern) { case 0: numberFormatInfo.CurrencyNegativePattern = 14; break; case 1: numberFormatInfo.CurrencyNegativePattern = 9; break; case 2: numberFormatInfo.CurrencyNegativePattern = 12; break; case 3: numberFormatInfo.CurrencyNegativePattern = 11; break; case 4: numberFormatInfo.CurrencyNegativePattern = 15; break; case 5: numberFormatInfo.CurrencyNegativePattern = 8; break; case 6: numberFormatInfo.CurrencyNegativePattern = 13; break; case 7: numberFormatInfo.CurrencyNegativePattern = 10; break; } } return(numberFormatInfo); }