|
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!
|
|
|
|
|
|
True -0 but the likelihood of the number system changing any time soon is low
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?
|
|
|
|
|
That's it - it's a doodle!
|
|
|
|
|
Marc A. Brown wrote: 1. Why not?
Well he said he did it at work, so I assumed it was something required for the business rather than being done as a leisure activity. So re-inventing the wheel is not generally a good thing for a business to do with so trivial a thing.
Marc A. Brown wrote: To learn the language. Beats the heck out of "Hello World".
Again - he was doing it at work & I assumed that he wasn't doing it for fun but for a business requirement.
Marc A. Brown wrote: 4. Why not?
Because
MVVM # - I did it My Way
___________________________________________
Man, you're a god. - walterhevedeich 26/05/2011
.\\axxx
(That's an 'M')
|
|
|
|
|
_Maxxx_ wrote: he said he did it at work, so I assumed it was something required for the
business
I did most of my Turing Machine experiments at a previous job.
|
|
|
|
|
I love code reviews. Very brave. The hyenas are moving in.
Peter Wasser
Art is making something out of nothing and selling it.
Frank Zappa
|
|
|
|
|
P.S.
Maybe look into using a StringBuilder
It's too early for a Friday Programming Quiz.
|
|
|
|
|
Oh oh, that's not how you do phone numbers.
I wanna be a eunuchs developer! Pass me a bread knife!
|
|
|
|
|
|
Phone numbers aren't numbers.
|
|
|
|
|
How about to roman numerals
|
|
|
|
|
Oh, yeah - that's fun - actually quite difficult!
|
|
|
|
|
Only as difficult as you like.
|
|
|
|
|
I added that to my version last night.
And today I wondered about adding support for doing thousands as hundreds, e.g. "twenty-five hundred".
|
|
|
|
|
Thanks for a few days' exercise. I made one that can be used like:
2345L.ToString<ShortScale>() ==> Two Thousand Three Hundred Forty-Five
Where ShortScale is:
public enum ShortScale : long
{
Zero = 0 , One = 1 , Two = 2 , Three = 3 , Four = 4 , Five = 5 , Six = 6 , Seven = 7 , Eight = 8
, Nine = 9 , Ten = 10 , Eleven = 11 , Twelve = 12 , Thirteen = 13 , Fourteen = 14 , Fifteen = 15
, Sixteen = 16 , Seventeen = 17 , Eighteen = 18 , Nineteen = 19
, [PIEBALD.Types.NumNuts.HyphenatedAttribute] Twenty = 20
...
, [PIEBALD.Types.NumNuts.CountedAttribute] Hundred = 100
...
}
I also have an enum that will yield Twenty-Three Hundred Forty-Five , but it needs work.
Here's another:
2345L.ToString<Roman>() ==> MMCCCXLV
public enum Roman
{
Nulla = 0
, [PIEBALD.Types.NumNuts.RepeatedAttribute] I = 1
, [PIEBALD.Types.NumNuts.RepeatedAttribute] IV = 4
, [PIEBALD.Types.NumNuts.RepeatedAttribute] V = 5
, [PIEBALD.Types.NumNuts.RepeatedAttribute] IX = 9
, [PIEBALD.Types.NumNuts.RepeatedAttribute] X = 10
...
}
But this technique tops out at Quintillion (short scale) or Trillion (long scale), so another technique would be necessary to work with greater numbers.
It should be possible to define a class of BigInteger constants and use that instead.
Added:
public sealed class TestClass
{
public static readonly System.Numerics.BigInteger Zero = 0 ;
public static readonly System.Numerics.BigInteger One = 1 ;
...
[PIEBALD.Types.NumNuts.HyphenatedAttribute]
public static readonly System.Numerics.BigInteger Twenty = 20 ;
...
[PIEBALD.Types.NumNuts.CountedAttribute]
public static readonly System.Numerics.BigInteger Hundred = 100 ;
...
[PIEBALD.Types.NumNuts.CountedAttribute]
public static readonly System.Numerics.BigInteger Decillion = Thousand * Nonillion ;
...
}
modified 30-Sep-13 23:49pm.
|
|
|
|
|
That's really interesting... I completely didn't think about doing it like that...
|
|
|
|
|
|
|
Brilliant! That put a grin on my face this morning!
|
|
|
|
|
I'm thinking about starting an open project, and I've been looking for a site to write about it, to include code. I've noticed a lot of blogs don't deal well with code.
Would articles here be an acceptable way to share the project?
|
|
|
|
|
|