Skip to content

Commit cc6734b

Browse files
author
Yulian Ashikov
committed
PR review comments
1 parent 8027be8 commit cc6734b

3 files changed

Lines changed: 38 additions & 18 deletions

File tree

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,25 @@ Or via the .NET Core command line interface:
4242
dotnet add package OneBitSoftware.Slovom
4343
```
4444

45+
## Usage
46+
47+
To use the library, call the `NumbersToWords.Convert` method, passing the amount and the desired currency descriptor.
48+
49+
```csharp
50+
using OneBitSoftware.Slovom;
51+
using OneBitSoftware.Slovom.Currencies;
52+
53+
// Convert BGN
54+
decimal amountBgn = 1234.56m;
55+
string resultBgn = NumbersToWords.Convert(amountBgn, CurrencyDescriptor.Bgn);
56+
// Result: "хиляда двеста тридесет и четири лева и 56 ст."
57+
58+
// Convert EUR
59+
decimal amountEur = 1234.56m;
60+
string resultEur = NumbersToWords.Convert(amountEur, CurrencyDescriptor.Euro);
61+
// Result: "хиляда двеста тридесет и четири евро и 56 ц."
62+
```
63+
4564
## Examples
4665

4766
## BGN examples

src/OneBitSoftware.Slovom/NumbersToWords.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -136,38 +136,38 @@ private static string AppendNegativePrefix(string numberAsWords, bool isNegative
136136

137137
public static string Convert(decimal number, CurrencyDescriptor currencyDescriptor)
138138
{
139-
if (number < -99999.99m) throw new ArgumentOutOfRangeException(nameof(number), "Входното число не може да бъде по-малко от -99999.99");
139+
if (number is < -99999.99m or > 99999.99m) throw new ArgumentOutOfRangeException(nameof(number), "Входното число трябва да бъде в интервала [-99999.99; 99999.99]");
140140
ArgumentNullException.ThrowIfNull(currencyDescriptor);
141141

142142
if (number is 0 or 0.0m) return currencyDescriptor.Vocabulary.NumbersZeroToNineteen[0] + currencyDescriptor.MajorCurrencyUnitPlural; // нула лева, нула евро
143143

144144
var isNegativeNumber = number < 0;
145145
number = Math.Abs(number); // Convert negative number to positive
146146

147-
var leva = (int)number;
148-
var stotinki = (int)((number % 1.0m) * 100);
147+
var majorUnit = (int)number;
148+
var minorUnit = (int)((number % 1.0m) * 100);
149149

150-
if (number == 1 && stotinki == 0) return AppendNegativePrefix(currencyDescriptor.Vocabulary.NumbersZeroToNineteen[leva] + currencyDescriptor.MajorCurrencyUnitSingular, isNegativeNumber); // един лев, едно евро
150+
if (number == 1 && minorUnit == 0) return AppendNegativePrefix(currencyDescriptor.Vocabulary.NumbersZeroToNineteen[majorUnit] + currencyDescriptor.MajorCurrencyUnitSingular, isNegativeNumber); // един лев, едно евро
151151

152-
var levaWords = leva != 1 ? ConvertWholeNumber(leva, currencyDescriptor.Vocabulary) + currencyDescriptor.MajorCurrencyUnitPlural : currencyDescriptor.Vocabulary.NumbersZeroToNineteen[leva] + currencyDescriptor.MajorCurrencyUnitSingular;
152+
var majorUnitWords = majorUnit != 1 ? ConvertWholeNumber(majorUnit, currencyDescriptor.Vocabulary) + currencyDescriptor.MajorCurrencyUnitPlural : currencyDescriptor.Vocabulary.NumbersZeroToNineteen[majorUnit] + currencyDescriptor.MajorCurrencyUnitSingular;
153153

154-
string stotinkiWords;
154+
string minorUnitWords;
155155

156-
if (leva == 0)
156+
if (majorUnit == 0)
157157
{
158-
if (stotinki == 0) return AppendNegativePrefix(currencyDescriptor.Vocabulary.NumbersZeroToNineteen[leva] + currencyDescriptor.MajorCurrencyUnitPlural, isNegativeNumber);
159-
if (stotinki == 1) return AppendNegativePrefix(currencyDescriptor.Vocabulary.MinorCurrencyUnitSingular + currencyDescriptor.MinorCurrencyUnitSingular, isNegativeNumber);
160-
if (stotinki == 2) return AppendNegativePrefix("две" + currencyDescriptor.MinorCurrencyUnitPlural, isNegativeNumber);
161-
if (stotinki == 10) return AppendNegativePrefix(currencyDescriptor.Vocabulary.NumbersZeroToNineteen[stotinki] + currencyDescriptor.MinorCurrencyUnitPlural, isNegativeNumber);
162-
if (stotinki < 20) return AppendNegativePrefix(currencyDescriptor.Vocabulary.NumbersZeroToNineteen[stotinki] + currencyDescriptor.MinorCurrencyUnitPlural, isNegativeNumber);
158+
if (minorUnit == 0) return AppendNegativePrefix(currencyDescriptor.Vocabulary.NumbersZeroToNineteen[majorUnit] + currencyDescriptor.MajorCurrencyUnitPlural, isNegativeNumber);
159+
if (minorUnit == 1) return AppendNegativePrefix(currencyDescriptor.Vocabulary.MinorCurrencyUnitSingular + currencyDescriptor.MinorCurrencyUnitSingular, isNegativeNumber);
160+
if (minorUnit == 2) return AppendNegativePrefix("две" + currencyDescriptor.MinorCurrencyUnitPlural, isNegativeNumber);
161+
if (minorUnit == 10) return AppendNegativePrefix(currencyDescriptor.Vocabulary.NumbersZeroToNineteen[minorUnit] + currencyDescriptor.MinorCurrencyUnitPlural, isNegativeNumber);
162+
if (minorUnit < 20) return AppendNegativePrefix(currencyDescriptor.Vocabulary.NumbersZeroToNineteen[minorUnit] + currencyDescriptor.MinorCurrencyUnitPlural, isNegativeNumber);
163163

164-
stotinkiWords = stotinki + " " + currencyDescriptor.MinorCurrencyUnitAbbreviated;
164+
minorUnitWords = minorUnit + " " + currencyDescriptor.MinorCurrencyUnitAbbreviated;
165165
}
166-
else stotinkiWords = stotinki + " " + currencyDescriptor.MinorCurrencyUnitAbbreviated;
166+
else minorUnitWords = minorUnit + " " + currencyDescriptor.MinorCurrencyUnitAbbreviated;
167167

168-
if (leva == 0) return AppendNegativePrefix(stotinkiWords, isNegativeNumber);
169-
if (stotinki == 0) return AppendNegativePrefix(levaWords, isNegativeNumber);
168+
if (majorUnit == 0) return AppendNegativePrefix(minorUnitWords, isNegativeNumber);
169+
if (minorUnit == 0) return AppendNegativePrefix(majorUnitWords, isNegativeNumber);
170170

171-
return AppendNegativePrefix(levaWords + " и " + stotinkiWords, isNegativeNumber);
171+
return AppendNegativePrefix(majorUnitWords + " и " + minorUnitWords, isNegativeNumber);
172172
}
173173
}

src/Tests/ConvertTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ public void NumberToWordsEuro_ShouldReturnCorrectWordsForNegativeValues(decimal
188188

189189
[Theory]
190190
[InlineData(-100000)]
191-
public void Convert_WithNumberLessThanNegativeLimit_ShouldThrowArgumentOutOfRangeException(decimal number)
191+
[InlineData(100000)]
192+
public void Convert_WithNumberOutOfTheSupportedLimits_ShouldThrowArgumentOutOfRangeException(decimal number)
192193
{
193194
Assert.Throws<ArgumentOutOfRangeException>(() => NumbersToWords.Convert(number, CurrencyDescriptor.Bgn));
194195
Assert.Throws<ArgumentOutOfRangeException>(() => NumbersToWords.Convert(number, CurrencyDescriptor.Euro));

0 commit comments

Comments
 (0)