/// <summary> /// Converts to words as per defined option /// </summary> /// <param name="number"></param> /// <returns></returns> public string ToWords(decimal number) { if (number <= 0) { return(Empty); } decimal fractionalDigits = number % 1; string integralDigitsString = number .ToString(CultureInfo.InvariantCulture) .Split('.') .ElementAt(0); string fractionalDigitsString = fractionalDigits.ToString(_options.DecimalPlaces > -1 ? $"F{_options.DecimalPlaces}" : "G", CultureInfo.InvariantCulture) .Split('.') .ElementAtOrDefault(1) ?? Empty; if (decimal.Parse(integralDigitsString) <= 0 && decimal.Parse(fractionalDigitsString) <= 0) { return(Empty); } string integralWords = Empty; if (decimal.Parse(integralDigitsString) > 0) { integralWords = _conversionFactory.ConvertDigits(integralDigitsString); integralWords = _options.CurrencyNotationType == NotationType.Prefix ? _options.CurrencyUnit + " " + integralWords : integralWords + " " + _options.CurrencyUnit; } if (int.Parse(fractionalDigitsString) <= 0 || IsNullOrEmpty(fractionalDigitsString)) { return(Concat(integralWords, (IsNullOrEmpty(_options.EndOfWordsMarker) ? "" : " " + _options.EndOfWordsMarker)).CapitalizeFirstLetter()); } string fractionalWords = _conversionFactory.ConvertDigits(fractionalDigitsString); fractionalWords = _options.SubCurrencyNotationType == NotationType.Prefix ? _options.SubCurrencyUnit + " " + fractionalWords : fractionalWords + " " + _options.SubCurrencyUnit; fractionalWords = ( $"{integralWords} " + $"{(IsNullOrEmpty(_options.CurrencyUnitSeparator) ? "" : _options.CurrencyUnitSeparator + " ")}" + $"{fractionalWords.TrimEnd()}{(IsNullOrEmpty(_options.EndOfWordsMarker) ? "" : " " + _options.EndOfWordsMarker)}") .Trim().CapitalizeFirstLetter(); return(fractionalWords); }
/// <summary> /// Convert number to words as per specified options /// </summary> /// <param name="number"></param> /// <returns></returns> public string ToWords(decimal number) { string integralDigitsString = number .ToString(CultureInfo.InvariantCulture) .Split('.') .ElementAt(0); decimal fractionalDigits = number % 1; string integralWords = _conversionFactory.ConvertDigits(integralDigitsString); string fractionalDigitsString = (_options.DecimalPlaces > -1 ? decimal.Parse(fractionalDigits.ToString($"F{_options.DecimalPlaces}", CultureInfo.InvariantCulture)) .ToString($"G{_options.DecimalPlaces}", CultureInfo.InvariantCulture) : fractionalDigits.ToString("G", CultureInfo.InvariantCulture) ) .Split('.') .ElementAtOrDefault(1); if (fractionalDigits <= 0 || IsNullOrEmpty(fractionalDigitsString)) { return(integralWords.CapitalizeFirstLetter()); } string fractionalWords = Empty; fractionalDigitsString .ToList() .ForEach(x => fractionalWords += _conversionFactory.ToOnesWords(Convert.ToUInt16(x.ToString(CultureInfo.InvariantCulture))) + " "); return($"{integralWords} {_options.DecimalSeparator} {fractionalWords.TrimEnd()}".CapitalizeFirstLetter()); }