Converting integers to roman numerals

Try this, simple and compact: public static string ToRoman(int number) { if ((number < 0) || (number > 3999)) throw new ArgumentOutOfRangeException(“insert value betwheen 1 and 3999”); if (number < 1) return string.Empty; if (number >= 1000) return “M” + ToRoman(number – 1000); if (number >= 900) return “CM” + ToRoman(number – 900); if (number … Read more

Converting Integers to Roman Numerals – Java

A compact implementation using Java TreeMap and recursion: import java.util.TreeMap; public class RomanNumber { private final static TreeMap<Integer, String> map = new TreeMap<Integer, String>(); static { map.put(1000, “M”); map.put(900, “CM”); map.put(500, “D”); map.put(400, “CD”); map.put(100, “C”); map.put(90, “XC”); map.put(50, “L”); map.put(40, “XL”); map.put(10, “X”); map.put(9, “IX”); map.put(5, “V”); map.put(4, “IV”); map.put(1, “I”); } public final … Read more

Convert a number into a Roman numeral in JavaScript

There is a nice one here on this blog I found using google: JavaScript Roman Numeral Converter function romanize (num) { if (isNaN(num)) return NaN; var digits = String(+num).split(“”), key = [“”,”C”,”CC”,”CCC”,”CD”,”D”,”DC”,”DCC”,”DCCC”,”CM”, “”,”X”,”XX”,”XXX”,”XL”,”L”,”LX”,”LXX”,”LXXX”,”XC”, “”,”I”,”II”,”III”,”IV”,”V”,”VI”,”VII”,”VIII”,”IX”], roman = “”, i = 3; while (i–) roman = (key[+digits.pop() + (i * 10)] || “”) + roman; return Array(+digits.join(“”) … Read more

How can you customize the numbers in an ordered list?

This is the solution I have working in Firefox 3, Opera and Google Chrome. The list still displays in IE7 (but without the close bracket and left align numbers): ol { counter-reset: item; margin-left: 0; padding-left: 0; } li { display: block; margin-bottom: .5em; margin-left: 2em; } li::before { display: inline-block; content: counter(item) “) “; … Read more

How do you match only valid roman numerals with a regular expression?

You can use the following regex for this: ^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$ Breaking it down, M{0,4} specifies the thousands section and basically restrains it to between 0 and 4000. It’s a relatively simple: 0: <empty> matched by M{0} 1000: M matched by M{1} 2000: MM matched by M{2} 3000: MMM matched by M{3} 4000: MMMM matched by M{4} … Read more