程序员

将阿拉伯数转为罗马数

2017-08-01  本文已影响0人  洒了油

罗马数字是样子的:

现在要将一个 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('');

}

上一篇下一篇

猜你喜欢

热点阅读