public void IsValidTest(string cultureInfoStr, string amountInNumbers, bool expected) { CultureInfo cultureInfo = new CultureInfo(cultureInfoStr); CurrencyValidator currencyValidator = new CurrencyValidator(cultureInfo, 6, 2); Assert.Equal(currencyValidator.IsValid(amountInNumbers), expected); }
public string ConvertCurrencyToWords(decimal currency) { if (!CurrencyValidator.IsValid(currency)) { throw new InvalidOperationException("Currency is out of range!"); } int dollars = (int)Math.Floor(currency); int cents = (int)((currency - dollars) * 100); return($"{GetDollarPartString(dollars)}{GetCentsPartString(cents)}"); }
/// <summary> /// Converts numbers to english speaking text. /// </summary> /// <param name="amountInNumbers">Eg: $123,567.11 or $123.567,11 depending on your culture</param> /// <returns>For english speking words for given number. For instance: one quadrillion two hundred thirty four trillion five hundred sixty seven billion eight hundred ninety one million two hundred thirty four thousand five hundred sixty seven</returns> public string NumberToWords(string amountInNumbers) { if (!currencyValidator.IsValid(amountInNumbers)) { throw new ArgumentException($"{nameof(amountInNumbers)} is not a valid money numbers"); } amountInNumbers = amountInNumbers.Replace(cultureInfo.NumberFormat.CurrencySymbol, string.Empty).ToLower().Trim(); List <string> dollarsAndCents = amountInNumbers.Split(cultureInfo.NumberFormat.CurrencyDecimalSeparator.ToCharArray()[0]).ToList(); string amountIntText = ""; amountIntText = $"{NumberConverter.NumberToText(long.Parse(dollarsAndCents[0].Replace(cultureInfo.NumberFormat.CurrencyGroupSeparator, "")))} dollar(s)"; if (dollarsAndCents.Count > 1) { amountIntText += $" {NumberConverter.NumberToText(long.Parse(dollarsAndCents[1]))} cent(s)"; } return(amountIntText); }
/// <summary> /// Sets the value of the IsValid property. /// </summary> private void SetIsValid() { IsValid = CurrencyValidator.IsValid(USDCurrency); }