罗马数字是样子的:
现在要将一个 1~3999这样一个数转成罗马数字,用javascript来实现:
function convertToRoman(num) {
var l4 = new Array('M','MM','MMM','MMMM');
var l3 = new Array('C','CC','CCC','CCCD','D','DC','DCC','DCCC','CM');
var l2 = new Array('X','XX','XXX','XL','L','LX','LXX','LXXX','XC');
var l1 = new Array('I','II','III','IV','V','VI','VII','VIII','IX');
var arrRoman = new Array();
arrRoman.push(l1);
arrRoman.push(l2);
arrRoman.push(l3);
arrRoman.push(l4);
num=num+"";
var numArr = num.split('');
numArr = numArr.reverse();
for(var i=0;i<numArr.length;i++){
var j = numArr[i];
numArr[i] = arrRoman[i][j-1];
}
numArr = numArr.reverse();
return numArr.join('');
}
网友评论