Currently Reading

Image of Derivatives Demystified: A Step-by-Step Guide to Forwards, Futures, Swaps and Options (The Wiley Finance Series)

Image of Working Effectively with Legacy Code (Robert C Martin)

Image of Coders at Work: Reflections on the Craft of Programming

Image of Succeeding with Agile: Software Development Using Scrum (Addison-Wesley Signature)

Image of iPhone UK: The Missing Manual

Image of My Shit Life So Far

Recently Read

Image of iPhone Advanced Projects (Books for Professionals by Professionals)

Image of The Secrets of Consulting: A Guide to Giving and Getting Advice Successfully

Image of Clean Code: A Handbook of Agile Software Craftsmanship (Robert C. Martin)

Image of Advanced Actionscript 3 with Design Patterns

Image of Beginning iPhone 3 Development: Exploring the iPhone SDK

Image of Test Driven Development (The Addison-Wesley Signature Series)

Image of ActionScript 3.0 Design Patterns: Object Oriented Programming Techniques (Adobe Developer Library)

Image of Refactoring: Improving the Design of Existing Code (Object Technology Series)

Image of UML 2 for Dummies

Image of Test Driven: TDD and Acceptance TDD for Java Developers


January 27, 2009

Number Romanizer

Filed under: ActionScript, Flex — Anthony @ 2:58 am

I was messing around on a personal project today and had to build a simple converter to change numbers into Roman Numerals. Below is the result. Had a few issues trying to use a simple object to hold the numerals as ActionScript was sorting the object Alphabetically which meant I was getting false results especially with 4's which would return four 'I''s instead of the correct 'IV'.

 
/**
* Function that turns any number into it's roman numeral
* equivelant.
* We have to use an array to hold the values as if we use
* a Dictionary / Object actionScript sorts it alphabetically.
* and we don't get the correct results.
*
* @param value : Number 0 - 9999;
*/
public static function romanize( value : Number ) : String
{
var numerals : Array = [
{ label : 'M', value : 1000 },
{ label : 'CM', value : 900 },
{ label : 'D', value : 500 },
{ label : 'CD', value : 400 },
{ label : 'C', value : 100 },
{ label : 'XC', value : 90 },
{ label : 'L', value : 50 },
{ label : 'XL', value : 40 },
{ label : 'X', value : 10 },
{ label : 'IX', value : 9 },
{ label : 'V', value : 5 },
{ label : 'IV', value : 4 },
{ label : 'I', value : 1 }
]
 
var roman : String = '';
 
for( var i : String in numerals )
{
while( value >= numerals[ i ].value )
{
roman += numerals[ i ].label;
value -= numerals[ i ].value;
}
}
 
return roman;
}

3 Comments »

  1. You forgot L and messed up XL/X

    Comment by Yo — February 13, 2009 @ 1:20 pm

  2. …and it will fail for values >= 2000 and values ending with 2,3,7 and 8 (you need to repeat those M’s and I’s)

    Comment by Yo — February 13, 2009 @ 1:31 pm

  3. Thanks for the info, I didn’t copy it over correctly :( as for the values not ending with 2,3,7 and 8 or being above 2000 can you give an example number? I have tried lots of different combinations and they all seem to work for me.
    Thanks
    Anthony

    Comment by Anthony — February 16, 2009 @ 12:38 pm

RSS feed for comments on this post. | TrackBack URI

Leave a comment

XHTML ( You can use these tags): <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .