Skip to content

Commit 7423699

Browse files
author
Yulian Ashikov
committed
Added support for negative numbers
1 parent c6c8e88 commit 7423699

1 file changed

Lines changed: 24 additions & 16 deletions

File tree

src/OneBitSoftware.Slovom/NumbersToWords.cs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@ private static string Hundreds(int n, NumberWordsVocabulary numberWordsVocabular
3030
var i = n / 100;
3131
var d = n % 100;
3232

33-
switch (n)
33+
if (n < 120)
3434
{
35-
case < 120: return numberWordsVocabulary.HundredsMultiples[i] + " и " + numberWordsVocabulary.NumbersZeroToNineteen[d];
36-
case < 200: return numberWordsVocabulary.HundredsMultiples[i] + " " + ConvertWholeNumber(d, numberWordsVocabulary);
35+
if (d == 0) return numberWordsVocabulary.HundredsMultiples[i];
36+
37+
return numberWordsVocabulary.HundredsMultiples[i] + " и " + numberWordsVocabulary.NumbersZeroToNineteen[d];
3738
}
3839

40+
if (n < 200) return numberWordsVocabulary.HundredsMultiples[i] + " " + ConvertWholeNumber(d, numberWordsVocabulary);
41+
3942
if (d < 20) return numberWordsVocabulary.HundredsMultiples[i] + (d == 0 ? "" : " и " + ConvertWholeNumber(d, numberWordsVocabulary));
4043

4144
return numberWordsVocabulary.HundredsMultiples[i] + (d == 0 ? "" : " " + ConvertWholeNumber(d, numberWordsVocabulary));
@@ -124,41 +127,46 @@ private static string TensOfThousands(int number, NumberWordsVocabulary numberWo
124127

125128
private static string BuildThousandsWithAnd(string thousands, string afterThousands) => thousands + " хиляди и " + afterThousands;
126129

130+
private static string AppendNegativePrefix(string numberAsWords, bool isNegativeNumber)
131+
{
132+
if (!isNegativeNumber) return numberAsWords;
133+
134+
return "Минус " + numberAsWords;
135+
}
136+
127137
public static string Convert(decimal number, CurrencyDescriptor currencyDescriptor)
128138
{
129139
ArgumentNullException.ThrowIfNull(currencyDescriptor);
130140

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

143+
var isNegativeNumber = number < 0;
133144
number = Math.Abs(number); // Convert negative number to positive
134145

135146
var leva = (int)number;
136147
var stotinki = (int)((number % 1.0m) * 100);
137148

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

140151
var levaWords = leva != 1 ? ConvertWholeNumber(leva, currencyDescriptor.Vocabulary) + currencyDescriptor.MajorCurrencyUnitPlural : currencyDescriptor.Vocabulary.NumbersZeroToNineteen[leva] + currencyDescriptor.MajorCurrencyUnitSingular;
141152

142153
string stotinkiWords;
143154

144155
if (leva == 0)
145156
{
146-
if (stotinki == 0) return currencyDescriptor.Vocabulary.NumbersZeroToNineteen[leva] + currencyDescriptor.MajorCurrencyUnitPlural;
147-
if (stotinki == 1) return currencyDescriptor.Vocabulary.MinorCurrencyUnitSingular + currencyDescriptor.MinorCurrencyUnitSingular;
148-
if (stotinki == 2) return "две" + currencyDescriptor.MinorCurrencyUnitPlural;
149-
if (stotinki == 10) return currencyDescriptor.Vocabulary.NumbersZeroToNineteen[stotinki] + currencyDescriptor.MinorCurrencyUnitPlural;
150-
if (stotinki < 20) return currencyDescriptor.Vocabulary.NumbersZeroToNineteen[stotinki] + currencyDescriptor.MinorCurrencyUnitPlural;
157+
if (stotinki == 0) return AppendNegativePrefix(currencyDescriptor.Vocabulary.NumbersZeroToNineteen[leva] + currencyDescriptor.MajorCurrencyUnitPlural, isNegativeNumber);
158+
if (stotinki == 1) return AppendNegativePrefix(currencyDescriptor.Vocabulary.MinorCurrencyUnitSingular + currencyDescriptor.MinorCurrencyUnitSingular, isNegativeNumber);
159+
if (stotinki == 2) return AppendNegativePrefix("две" + currencyDescriptor.MinorCurrencyUnitPlural, isNegativeNumber);
160+
if (stotinki == 10) return AppendNegativePrefix(currencyDescriptor.Vocabulary.NumbersZeroToNineteen[stotinki] + currencyDescriptor.MinorCurrencyUnitPlural, isNegativeNumber);
161+
if (stotinki < 20) return AppendNegativePrefix(currencyDescriptor.Vocabulary.NumbersZeroToNineteen[stotinki] + currencyDescriptor.MinorCurrencyUnitPlural, isNegativeNumber);
151162

152163
stotinkiWords = stotinki + " " + currencyDescriptor.MinorCurrencyUnitAbbreviated;
153164
}
154-
else
155-
{
156-
stotinkiWords = stotinki + " " + currencyDescriptor.MinorCurrencyUnitAbbreviated;
157-
}
165+
else stotinkiWords = stotinki + " " + currencyDescriptor.MinorCurrencyUnitAbbreviated;
158166

159-
if (leva == 0) return stotinkiWords;
160-
if (stotinki == 0) return levaWords;
167+
if (leva == 0) return AppendNegativePrefix(stotinkiWords, isNegativeNumber);
168+
if (stotinki == 0) return AppendNegativePrefix(levaWords, isNegativeNumber);
161169

162-
return levaWords + " и " + stotinkiWords;
170+
return AppendNegativePrefix(levaWords + " и " + stotinkiWords, isNegativeNumber);
163171
}
164172
}

0 commit comments

Comments
 (0)