仅仅适用于小于3999的数,如果大于3999 就堆M吧
function convert(num) {
let roma = "";
if(num < 0 || num >3999) return roma;
let count = (num + "").length;//几位数
let l = 0;
while(count > 0){
if(num >= 10){
l = Math.floor(num / Math.pow(10,count - 1));
}else{
l = num;
count = 1;
}
roma = roma + getRoma(l,count);
if(num > 0)
num = num - l * Math.pow(10,count - 1);
count --;
}
return roma;
}
function getRoma(num,count){
let str = "";
if(num === 0)return str;
if(num > 0 && num <= 3){
while(num > 0){
str += getRomaOne(Math.pow(10,count - 1));
num --;
}
}else if(num === 4){
str += (getRomaOne(Math.pow(10,count - 1)) + getRomaOne(5 * Math.pow(10,count - 1)));
}else if(num <= 8){
str += getRomaOne(5 * Math.pow(10,count - 1))
while(num > 5){
str += getRomaOne(Math.pow(10,count - 1));
num --;
}
}else if(num === 9){
str += (getRomaOne(Math.pow(10,count - 1)) + getRomaOne(Math.pow(10,count)));
}
return str;
}
function getRomaOne(num){
switch(num){
case 1:
return "I";
case 5:
return "V";
case 10:
return "X";
case 50:
return "L";
case 100:
return "C";
case 500:
return "D";
case 1000:
return "M"
default:
return ""
}
}