Skip to content

Commit

Permalink
tidying up ToWords converter particularly Spanish
Browse files Browse the repository at this point in the history
  • Loading branch information
MehdiK committed Apr 11, 2014
1 parent 700c539 commit a0d2c26
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,6 @@ public interface IFormatter
string TimeSpanHumanize_Zero();
}

public class EnglishNumberToWordsConverter
{
public EnglishNumberToWordsConverter() { }
public string Convert(int number) { }
public string ConvertToOrdinal(int number) { }
}

public interface INumberToWordsConverter
{
string Convert(int number);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Humanizer.Localisation.NumberToWords
{
public class EnglishNumberToWordsConverter : INumberToWordsConverter
internal class EnglishNumberToWordsConverter : INumberToWordsConverter
{
private static readonly string[] UnitsMap = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
private static readonly string[] TensMap = { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using Humanizer.Localisation.NumberToWords;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Humanizer {
public class SpanishNumberToWordsConverter : INumberToWordsConverter {
namespace Humanizer.Localisation.NumberToWords
{
internal class SpanishNumberToWordsConverter : INumberToWordsConverter
{
private static readonly string[] HundredsMap = { "cero", "ciento", "doscientos", "trescientos", "cuatrocientos", "quinientos", "seiscientos", "setecientos", "ochocientos", "novecientos" };
private static readonly string[] UnitsMap = { "cero", "uno", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve", "diez", "once", "doce", "trece", "catorce", "quince", "dieciséis", "diecisiete", "dieciocho", "diecinueve" };
private static readonly string[] TensMap = { "cero", "diez", "veinte", "treinta", "cuarenta", "cincuenta", "sesenta", "setenta", "ochenta", "noventa" };

public string Convert(int number)
public string Convert(int number)
{
if (number == 0)
return "cero";
Expand All @@ -20,55 +19,52 @@ public string Convert(int number)

var parts = new List<string>();

if ((number / 1000000000) > 0)
if ((number / 1000000000) > 0)
{
if (number / 1000000000 == 1)
parts.Add(string.Format("mil millones", Convert(number / 1000000000)));
else
parts.Add(string.Format("{0} mil millones", Convert(number / 1000000000)));
parts.Add(number/1000000000 == 1
? string.Format("mil millones")
: string.Format("{0} mil millones", Convert(number/1000000000)));

number %= 1000000000;
}

if ((number / 1000000) > 0)
if ((number / 1000000) > 0)
{
if (number / 1000000 == 1)
parts.Add(string.Format("millón", Convert(number / 1000000)));
else
parts.Add(string.Format("{0} millones", Convert(number / 1000000)));
parts.Add(number/1000000 == 1
? string.Format("millón")
: string.Format("{0} millones", Convert(number/1000000)));

number %= 1000000;
}

if ((number / 1000) > 0)
if ((number / 1000) > 0)
{
if (number / 1000 == 1)
parts.Add(string.Format("mil", Convert(number / 1000)));
else
parts.Add(string.Format("{0} mil", Convert(number / 1000)));
parts.Add(number/1000 == 1
? string.Format("mil")
: string.Format("{0} mil", Convert(number/1000)));

number %= 1000;
}

if ((number / 100) > 0)
if ((number / 100) > 0)
{
if (number == 100)
parts.Add(string.Format("cien", Convert(number / 100)));
else
parts.Add(HundredsMap[(number / 100)]);

parts.Add(number == 100 ? string.Format("cien") : HundredsMap[(number/100)]);
number %= 100;
}

if (number > 0)
if (number > 0)
{
if (number < 20)
parts.Add(UnitsMap[number]);
else if (number > 20 && number < 30) {
else if (number > 20 && number < 30)
{
var lastPart = TensMap[number / 10];
if ((number % 10) > 0)
lastPart += string.Format(" {0}", UnitsMap[number % 10]);

parts.Add(lastPart);
}
else
}
else
{
var lastPart = TensMap[number / 10];
if ((number % 10) > 0)
Expand All @@ -81,7 +77,7 @@ public string Convert(int number)
return string.Join(" ", parts.ToArray());
}

public string ConvertToOrdinal(int number)
public string ConvertToOrdinal(int number)
{
throw new NotImplementedException();
}
Expand Down

2 comments on commit a0d2c26

@thunsaker
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My autoformatting does the curly brace on same line thing, which is my preference, sometimes I miss a few while reverting them.

Good call with the ternary operators.

@MehdiK
Copy link
Member Author

@MehdiK MehdiK commented on a0d2c26 Apr 11, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review.

Haa! Good to know. I will add a ReSharper team setting so it's enforced on the codebase, at least for those who have R#.

Please sign in to comment.