Converting numbers into English
-
Or casting pearls before swine.
:confused:
The difficult we do right away... ...the impossible takes slightly longer.
-
PIEBALDconsult wrote:
Shows lack of clarity of thought
Or simple ignorance.
The difficult we do right away... ...the impossible takes slightly longer.
If he were doing VB I'd agree.
-
Because I felt like it. And I am my employer, so it was excellent value!
As a purely fun exercise it's the kind of thing I enjoy doing; I don't think it was good financial value regardless as to who the employer was - although keeping the staff happy is equally important ;)
MVVM # - I did it My Way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')
-
So I've been enjoying myself at work today: I wrote something to take a number and convert it into English, e.g. "12324.56" becomes "twelve thousand, three hundred and twenty-four point five six". I know this is an old problem: I remember having done this something like 30 years ago, as part of a computer science class, but I actually needed it for something today: in doing it, I was amazed at how many ways there are to achieve it in C# (the last time I wrote it, it was in Algol-60!), and how many little optimizations I was able to add as I sat there looking at each iteration of the code. I ended up trying to keep the code as terse as I could but also as fast as I could, without having too many IFs and things all over the place. I ended up using a bunch of enums and letting the runtime make words out of them, rather than having strings for it: I'm not sure it makes a huge difference, but it just seemed more elegant, somehow. Of course, in a problem like this, there's always the part about trying to stop it saying things like, "two thousand, zero hundred and onety-zero", so part of the fun was trying not to write anything too specific to avoid things like that: in my mind, if I got the algorithm right, that stuff would just sort of work... It's nice having a bit of time on one's hands at work, for a change. Anyway, I had a lot of fun, so I thought I'd share: if anyone else has a better method (and I'm sure they do) then why not join in...? Meanwhile, here's my version:
public static class Numeric
{
private enum Digit
{
zero = 0, one = 1, two = 2, three = 3, four = 4,
five = 5, six = 6, seven = 7, eight = 8, nine = 9
}private enum Teen
{
ten = 10, eleven = 11, twelve = 12, thirteen = 13, fourteen = 14,
fifteen = 15, sixteen = 16, seventeen = 17, eighteen = 18, nineteen = 19
}private enum Ten
{
twenty = 2, thirty = 3, forty = 4, fifty = 5,
sixty = 6, seventy = 7, eighty = 8, ninety = 9
}private enum PowerOfTen
{
hundred = 0, thousand = 1, million = 2, billion = 3,
trillion = 4, quadrillion = 5, quintillion = 6
}/// /// How many powers of ten there are; faster to work this out ahead of time,
/// and I didn't want to hard-code it into the algorithm...
///
private static int PowersOfTen = Enum.GetValues(typeof(PowerOfTen)).Length;/// /// Converts a number to English words
///
/// The numberP.S. Maybe look into using a StringBuilder It's too early for a Friday Programming Quiz.
-
As a purely fun exercise it's the kind of thing I enjoy doing; I don't think it was good financial value regardless as to who the employer was - although keeping the staff happy is equally important ;)
MVVM # - I did it My Way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')
Oh, yeah - I wouldn't bother unless I had a slow day: in this case, I did - it was purely for fun.
-
Needless really, it just wraps the Parse routines you should be using. Extra calls on the stack. Shows lack of clarity of thought.
A parser isn't really applicable here: you can't exactly parse a string of digits into English...
-
Oh, yeah - I wouldn't bother unless I had a slow day: in this case, I did - it was purely for fun.
You had to go no further than our very own CP[^] or I stole this from StuckOverflow...
public static string NumberToWords(int number)
{
if (number == 0)
return "zero";if (number < 0) return "minus " + NumberToWords(Math.Abs(number)); string words = ""; if ((number / 1000000) > 0) { words += NumberToWords(number / 1000000) + " million "; number %= 1000000; } if ((number / 1000) > 0) { words += NumberToWords(number / 1000) + " thousand "; number %= 1000; } if ((number / 100) > 0) { words += NumberToWords(number / 100) + " hundred "; number %= 100; } if (number > 0) { if (words != "") words += "and "; 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", "seventy", "eighty", "ninety" }; if (number < 20) words += unitsMap\[number\]; else { words += tensMap\[number / 10\]; if ((number % 10) > 0) words += "-" + unitsMap\[number % 10\]; } } return words;
}
MVVM # - I did it My Way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')
-
So I've been enjoying myself at work today: I wrote something to take a number and convert it into English, e.g. "12324.56" becomes "twelve thousand, three hundred and twenty-four point five six". I know this is an old problem: I remember having done this something like 30 years ago, as part of a computer science class, but I actually needed it for something today: in doing it, I was amazed at how many ways there are to achieve it in C# (the last time I wrote it, it was in Algol-60!), and how many little optimizations I was able to add as I sat there looking at each iteration of the code. I ended up trying to keep the code as terse as I could but also as fast as I could, without having too many IFs and things all over the place. I ended up using a bunch of enums and letting the runtime make words out of them, rather than having strings for it: I'm not sure it makes a huge difference, but it just seemed more elegant, somehow. Of course, in a problem like this, there's always the part about trying to stop it saying things like, "two thousand, zero hundred and onety-zero", so part of the fun was trying not to write anything too specific to avoid things like that: in my mind, if I got the algorithm right, that stuff would just sort of work... It's nice having a bit of time on one's hands at work, for a change. Anyway, I had a lot of fun, so I thought I'd share: if anyone else has a better method (and I'm sure they do) then why not join in...? Meanwhile, here's my version:
public static class Numeric
{
private enum Digit
{
zero = 0, one = 1, two = 2, three = 3, four = 4,
five = 5, six = 6, seven = 7, eight = 8, nine = 9
}private enum Teen
{
ten = 10, eleven = 11, twelve = 12, thirteen = 13, fourteen = 14,
fifteen = 15, sixteen = 16, seventeen = 17, eighteen = 18, nineteen = 19
}private enum Ten
{
twenty = 2, thirty = 3, forty = 4, fifty = 5,
sixty = 6, seventy = 7, eighty = 8, ninety = 9
}private enum PowerOfTen
{
hundred = 0, thousand = 1, million = 2, billion = 3,
trillion = 4, quadrillion = 5, quintillion = 6
}/// /// How many powers of ten there are; faster to work this out ahead of time,
/// and I didn't want to hard-code it into the algorithm...
///
private static int PowersOfTen = Enum.GetValues(typeof(PowerOfTen)).Length;/// /// Converts a number to English words
///
/// The numberOh oh, that's not how you do phone numbers.
I wanna be a eunuchs developer! Pass me a bread knife!
-
:confused:
The difficult we do right away... ...the impossible takes slightly longer.
I think maybe he didnt care about the difference I think if I'd been starting out trying to do this I might have started the same way - expecting beginners in C# (ie me) to know why you wouldnt use convert, vs parse is probably not so important as getting the job done - make it work, then make it pretty. Maybe in a test case I'd have found out about how Convert handles (cough) nulls in input and gone 'hmmm, that'll need to change' - not sure the speed difference would be noticeble in the areas - batch processing - that I'd be using it 'g'
-
So I've been enjoying myself at work today: I wrote something to take a number and convert it into English, e.g. "12324.56" becomes "twelve thousand, three hundred and twenty-four point five six". I know this is an old problem: I remember having done this something like 30 years ago, as part of a computer science class, but I actually needed it for something today: in doing it, I was amazed at how many ways there are to achieve it in C# (the last time I wrote it, it was in Algol-60!), and how many little optimizations I was able to add as I sat there looking at each iteration of the code. I ended up trying to keep the code as terse as I could but also as fast as I could, without having too many IFs and things all over the place. I ended up using a bunch of enums and letting the runtime make words out of them, rather than having strings for it: I'm not sure it makes a huge difference, but it just seemed more elegant, somehow. Of course, in a problem like this, there's always the part about trying to stop it saying things like, "two thousand, zero hundred and onety-zero", so part of the fun was trying not to write anything too specific to avoid things like that: in my mind, if I got the algorithm right, that stuff would just sort of work... It's nice having a bit of time on one's hands at work, for a change. Anyway, I had a lot of fun, so I thought I'd share: if anyone else has a better method (and I'm sure they do) then why not join in...? Meanwhile, here's my version:
public static class Numeric
{
private enum Digit
{
zero = 0, one = 1, two = 2, three = 3, four = 4,
five = 5, six = 6, seven = 7, eight = 8, nine = 9
}private enum Teen
{
ten = 10, eleven = 11, twelve = 12, thirteen = 13, fourteen = 14,
fifteen = 15, sixteen = 16, seventeen = 17, eighteen = 18, nineteen = 19
}private enum Ten
{
twenty = 2, thirty = 3, forty = 4, fifty = 5,
sixty = 6, seventy = 7, eighty = 8, ninety = 9
}private enum PowerOfTen
{
hundred = 0, thousand = 1, million = 2, billion = 3,
trillion = 4, quadrillion = 5, quintillion = 6
}/// /// How many powers of ten there are; faster to work this out ahead of time,
/// and I didn't want to hard-code it into the algorithm...
///
private static int PowersOfTen = Enum.GetValues(typeof(PowerOfTen)).Length;/// /// Converts a number to English words
///
/// The number -
What I would ask myself is: Why did you re-invent the wheel? Was this good value for money for your employer? There are many many solutions available to do this (for free) on the interwebs. Why did it need to be 'terse and fast' - surely better to be 'easily readable, debuggable and maintainable'?
MVVM # - I did it My Way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')
1. Why not? 2. To learn the language. Beats the heck out of "Hello World". 3. For the same reason people draw doodles. 4. Why not? :)
-
How about to roman numerals ;)
Software Kinetics - Dependable Software news
Oh, yeah - that's fun - actually quite difficult!
-
Oh oh, that's not how you do phone numbers.
I wanna be a eunuchs developer! Pass me a bread knife!
ROFL!
-
You had to go no further than our very own CP[^] or I stole this from StuckOverflow...
public static string NumberToWords(int number)
{
if (number == 0)
return "zero";if (number < 0) return "minus " + NumberToWords(Math.Abs(number)); string words = ""; if ((number / 1000000) > 0) { words += NumberToWords(number / 1000000) + " million "; number %= 1000000; } if ((number / 1000) > 0) { words += NumberToWords(number / 1000) + " thousand "; number %= 1000; } if ((number / 100) > 0) { words += NumberToWords(number / 100) + " hundred "; number %= 100; } if (number > 0) { if (words != "") words += "and "; 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", "seventy", "eighty", "ninety" }; if (number < 20) words += unitsMap\[number\]; else { words += tensMap\[number / 10\]; if ((number % 10) > 0) words += "-" + unitsMap\[number % 10\]; } } return words;
}
MVVM # - I did it My Way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')
Yeah - I thought about doing it that way but it wasn't fun to write! I'm sure I overcomplicated it, but - whatever - I had fun!
-
1. Why not? 2. To learn the language. Beats the heck out of "Hello World". 3. For the same reason people draw doodles. 4. Why not? :)
That's it - it's a doodle!
-
mark merrens wrote:
Curmudgeonprofessional
ftfy
MVVM # - I did it My Way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')
_Maxxx_ wrote:
Curmudgeonprofessional
Pompous ass. That should do it.
"If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. Those who seek perfection will only find imperfection nils illegitimus carborundum me, me, me me, in pictures
-
Really? Convert's slow? Thanks!
-
A parser isn't really applicable here: you can't exactly parse a string of digits into English...
Convert.ToInt32(N % p);
Use a cast. There is only one useful method in Convert -- ChangeType. For all other uses for Convert, there are better alternatives. -
Convert.ToInt32(N % p);
Use a cast. There is only one useful method in Convert -- ChangeType. For all other uses for Convert, there are better alternatives.Noted - very good.
-
_Maxxx_ wrote:
Curmudgeonprofessional
Pompous ass. That should do it.
"If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. Those who seek perfection will only find imperfection nils illegitimus carborundum me, me, me me, in pictures
Concur!