English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Anyone have javascript code to convert numeric values to AND FROM roman numerals? I have found several converters to roman but never from. Im even ok with conversions up to #40.

Thanks

2007-01-24 12:06:33 · 3 answers · asked by zerohourx 1 in Computers & Internet Programming & Design

3 answers

This script assumes any Roman Numeral you enter will be in the correct order. Validating a Roman Numeral looks like another exercise for me. :)


strRoman = prompt( "Enter Roman Numeral :","");
var total = 0;
var queue = 0;

while( strRoman.length > 0) {
strNextVal = strRoman.substring(0,1);
strRoman = strRoman.substring(1);

switch( strNextVal) {
case "M" : intNextVal = 1000; break;
case "D" : intNextVal = 500; break;
case "C" : intNextVal = 100; break;
case "L" : intNextVal = 50; break;
case "X" : intNextVal = 10; break;
case "V" : intNextVal = 5; break;
case "I" : intNextVal = 1; break;
default : alert("Invalid character : " + strNextVal); break;
}

if( queue) {
if( intNextVal <= queue) {
total += queue;
queue = intNextVal;
} else {
total += intNextVal - queue;
queue = 0;
}
} else {
queue = intNextVal;
}

}

total += queue;

document.write(total);

2007-01-24 16:04:17 · answer #1 · answered by Kookiemon 6 · 0 0

Julian calendar, Latin: Dies Veneris xxiv Maius MMVIII In Roman style, that's greater complicated: DIES VENERIS A.D. IX KAL. IVN. MMDCCLXI A.U.C. As somebody else referred to, some dates have been expressed as dates earlier yet another date, so twenty fourth might will become 9 (IX) earlier the Kalends (KAL) of June (IVN). AUC (ab urbe condita) counts the years from the beginning up of Rome you're able to discover people turning up on the incorrect date in case you exhibit the date interior the Roman way!

2016-11-27 00:07:56 · answer #2 · answered by cerchia 4 · 0 0

I wrote a Java program that will convert numbers from 1 to 100 to roman numeral and back again. I know it is not in Javascript like you wanted, but I'm sure you can reuse the logic. The toRoman method converts integers to a roman numeral string. The fromRoman method converts a roman numeral string to an integer.

package roman;

public class RomanConvert
{
public static void main(String[] args)
{
for( int index = 1; index <= 100; index++ )
{
String roman = toRoman( index );
int value = fromRoman( roman );

// The index should be the same as the value.
System.out.println( "From " + index + " => " + roman + " => " + value );
}
}

public static String toRoman( int value )
{
StringBuffer result = new StringBuffer();

if( value > 100 ) throw new IllegalArgumentException( "Value over 100." );
if( value < 1 ) throw new IllegalArgumentException( "Value is less than 1" );

while( value > 0 )
{
if( value == 100 )
{
result.append( "C" );
value = value - 100;
}
else if( value >= 90 && value < 100 )
{
result.append( "XC" );
value = value - 90;
}
else if( value >= 50 && value < 90 )
{
result.append( "L" );
value = value - 50;
}
else if( value >= 40 && value < 50 )
{
result.append( "XL" );
value = value - 40;
}
else if( value >= 10 && value < 40 )
{
result.append( "X" );
value = value - 10;
}
else if( value == 9 )
{
result.append( "IX" );
value = value - 9;
}
else if( value >= 5 && value < 9 )
{
result.append( "V" );
value = value - 5;
}
else if( value == 4 )
{
result.append( "IV" );
value = value - 4;
}
else
{
result.append( "I" );
value = value - 1;
}
}

return result.toString();
}

public static int fromRoman( String value )
{
int result = 0;
int index = 0;

if( value == null ) throw new IllegalArgumentException( "Null value" );

// Remove any spaces at the begining or the end and
// convert all the letters to uppercase.
value = value.trim().toUpperCase();

while( index < value.length() )
{
char curr = value.charAt( index );
char next = index + 1 < value.length() ? value.charAt( index + 1 ) : 0;
char next2 = index + 2 < value.length() ? value.charAt( index + 2 ) : 0;

if( curr == 'C' )
{
result = result + 100;
index = index + 1;

if( next != 0 )
{
throw new IllegalArgumentException( "Bad character:\'" + next + "\' found in string." );
}
}
else if( curr == 'X' && next == 'C' )
{
result = result + 90;
index = index + 2;

if( next2 != 0 && next2 != 'L' && next2 != 'X' && next2 != 'V' && next2 != 'I' )
{
throw new IllegalArgumentException( "Bad character:\'" + next2 + "\' found in string." );
}
}
else if( curr == 'L' )
{
result = result + 50;
index = index + 1;

if( next != 0 && next != 'X' && next != 'V' && next != 'I' )
{
throw new IllegalArgumentException( "Bad character:\'" + next + "\' found in string." );
}
}
else if( curr == 'X' && next == 'L' )
{
result = result + 40;
index = index + 2;

if( next2 != 0 && next2 != 'X' && next2 != 'V' && next2 != 'I' )
{
throw new IllegalArgumentException( "Bad character:\'" + next2 + "\' found in string." );
}
}
else if( curr == 'X' )
{
result = result + 10;
index = index + 1;

if( next != 0 && next != 'X' && next != 'V' && next != 'I' )
{
throw new IllegalArgumentException( "Bad character:\'" + next + "\' found in string." );
}
}
else if( curr == 'I' && next == 'X' )
{
result = result + 9;
index = index + 2;

if( next2 != 0 )
{
throw new IllegalArgumentException( "Bad character:\'" + next2 + "\' found in string." );
}
}
else if( curr == 'I' && next == 'V' )
{
result = result + 4;
index = index + 2;

if( next2 != 0 )
{
throw new IllegalArgumentException( "Bad character:\'" + next2 + "\' found in string." );
}
}
else if( curr == 'V' )
{
result = result + 5;
index = index + 1;

if( next != 0 && next != 'I' )
{
throw new IllegalArgumentException( "Bad character:\'" + next + "\' found in string." );
}
}
else if( curr == 'I' )
{
result = result + 1;
index = index + 1;

if( next != 0 && next != 'I' )
{
throw new IllegalArgumentException( "Bad character:\'" + next + "\' found in string." );
}
}
else
{
throw new IllegalArgumentException( "Bad character:\'" + curr + "\' found in string." );
}
}

return result;
}
}

2007-01-24 15:23:57 · answer #3 · answered by Peter M 2 · 0 0

fedest.com, questions and answers