Method of Converting Time to Word...................................
private static readonly string[] fuzzyHours = new string[]
{
"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Forteen", "FIFTEEN",
"Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Twenty-One", "Twenty-Two", "Twenty-Three"
};
..........................................................
public static string TimeToWords(DateTime time)
{
string result;
int minutes = time.Minute;
int hours = time.Hour;
if (minutes != 0)
result = fuzzyHours[hours] + " " + NumberToWords(minutes);
else
result = fuzzyHours[hours] + ((hours != 0) ? " O'Clock" : string.Empty);
if (hours >= 0 && hours < 12)
return result + " am";
if (hours >= 12)
result = result + " pm";
return result;
}
public static string NumberToWords(Int32 number)
{
if (number == 0)
return "zero";
if (number < 0)
return "minus " + NumberToWords(Math.Abs(number));
string words = "";
if (number > 0)
{
if (words != "")
words += " ";
var unitsMap = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
var tensMap = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty" };
if (number < 20)
words += unitsMap[number];
else
{
words += tensMap[number / 10];
if ((number % 10) > 0)
words += "-" + unitsMap[number % 10];
}
}
return words;
}
private static readonly string[] fuzzyHours = new string[]
{
"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Forteen", "FIFTEEN",
"Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Twenty-One", "Twenty-Two", "Twenty-Three"
};
..........................................................
public static string TimeToWords(DateTime time)
{
string result;
int minutes = time.Minute;
int hours = time.Hour;
if (minutes != 0)
result = fuzzyHours[hours] + " " + NumberToWords(minutes);
else
result = fuzzyHours[hours] + ((hours != 0) ? " O'Clock" : string.Empty);
if (hours >= 0 && hours < 12)
return result + " am";
if (hours >= 12)
result = result + " pm";
return result;
}
public static string NumberToWords(Int32 number)
{
if (number == 0)
return "zero";
if (number < 0)
return "minus " + NumberToWords(Math.Abs(number));
string words = "";
if (number > 0)
{
if (words != "")
words += " ";
var unitsMap = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
var tensMap = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty" };
if (number < 20)
words += unitsMap[number];
else
{
words += tensMap[number / 10];
if ((number % 10) > 0)
words += "-" + unitsMap[number % 10];
}
}
return words;
}
No comments:
Post a Comment