Skip to content

Commit

Permalink
tidied up BrazilianPortugueseNumberToWordsConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
MehdiK committed Apr 12, 2014
1 parent 052d702 commit 2267268
Showing 1 changed file with 14 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ public override string Convert(int number, GrammaticalGender gender)
parts.Add("e");

if (number < 20)
{
parts.Add(ApplyGender(PortugueseUnitsMap[number], gender));
}
else
{
var lastPart = PortugueseTensMap[number / 10];
Expand Down Expand Up @@ -126,21 +124,17 @@ public override string ConvertToOrdinal(int number, GrammaticalGender gender)
if ((number / 100) > 0)
{
parts.Add(ApplyOrdinalGender(PortugueseOrdinalHundredsMap[number / 100], gender));

number %= 100;
}

if ((number / 10) > 0)
{
parts.Add(ApplyOrdinalGender(PortugueseOrdinalTensMap[number / 10], gender));

number %= 10;
}

if (number > 0)
{
parts.Add(ApplyOrdinalGender(PortugueseOrdinalUnitsMap[number], gender));
}

return string.Join(" ", parts.ToArray());
}
Expand All @@ -152,31 +146,27 @@ public override string ConvertToOrdinal(int number)

private string ApplyGender(string toWords, GrammaticalGender gender)
{
if (gender == GrammaticalGender.Feminine)
if (toWords.EndsWith("os"))
return toWords.Substring(0, toWords.Length - 2) + "as";
else
{
if (toWords.EndsWith("um"))
return toWords.Substring(0, toWords.Length - 2) + "uma";
else
{
if (toWords.EndsWith("dois"))
return toWords.Substring(0, toWords.Length - 4) + "duas";
else
return toWords;
}
}
else
if (gender != GrammaticalGender.Feminine)
return toWords;

if (toWords.EndsWith("os"))
return toWords.Substring(0, toWords.Length - 2) + "as";

if (toWords.EndsWith("um"))
return toWords.Substring(0, toWords.Length - 2) + "uma";

if (toWords.EndsWith("dois"))
return toWords.Substring(0, toWords.Length - 4) + "duas";

return toWords;
}

private string ApplyOrdinalGender(string toWords, GrammaticalGender gender)
{
if (gender == GrammaticalGender.Feminine)
return toWords.TrimEnd('o') + 'a';
else
return toWords;

return toWords;
}
}
}

0 comments on commit 2267268

Please sign in to comment.