Mosquito Mike
Mosquito Mike

Reputation: 982

Converting a decimal to an English string

I have written a .NET class that converts decimal (money values) to an english readable string.

113.25 => one hundred thirteen and 25 cents.

It is the amount line on checks. The code is deployed and works, but I was wondering if there was a better approach than writing a bunch of if, switch, integer division, and modulus statements. I was hoping for a more elegant solution rather than my 168 lines of grind it out code.

Upvotes: 1

Views: 166

Answers (2)

Paul Suart
Paul Suart

Reputation: 6713

Whilst this is a common code golf problem, as the "fizz buzz" question on here proved: you don't always get good answers!

There's code available from the excellent Black Wasp resource: http://www.blackwasp.co.uk/NumberToWords.aspx that solves your problem. Maybe compare their approach to your own and see if you're on the right track.

Whilst their solution currently only handles integers, I'd imagine you could either adapt it very easily, or pass in your mantissa and exponent separately and combine the results.

Hope this helps.

Upvotes: 2

John MacIntyre
John MacIntyre

Reputation: 13021

I believe what you are looking for has been done in every language in Code Golf: Number to Words

Upvotes: 6

Related Questions