阿拉伯数字转罗马数字(javascript版)

2019/7/22 2:10:22 人评论 次浏览 分类:学习教程

原文链接:http://www.cnblogs.com/yoder/archive/2012/02/07/2340881.html
// Convert to Roman Numerals
// copyright 25th July 2005, by Stephen Chapman http://javascript.about.com
// permission to use this Javascript on your web page is granted
// provided that all of the code (including this copyright notice) is
// used exactly as shown
function roman(n,s) {
	var r = '';
	var d; 
	var rn = new Array('IIII','V','XXXX','L','CCCC','D','MMMM'); 
	for (var i=0; i< rn.length; i++) {
		var x = rn[i].length+1;
		var d = n%x; 
		r= rn[i].substr(0,d)+r;
		n = (n-d)/x;
	} 
	if (s) {
		r=r.replace(/DCCCC/g,'CM');
		r=r.replace(/CCCC/g,'CD');
		r=r.replace(/LXXXX/g,'XC');
		r=r.replace(/XXXX/g,'XL');
		r=r.replace(/VIIII/g,'IX');
		r=r.replace(/IIII/g,'IV');
	} 
	return r;
}

 

转载于:https://www.cnblogs.com/yoder/archive/2012/02/07/2340881.html

相关资讯

    暂无相关的资讯...

共有访客发表了评论 网友评论

验证码: 看不清楚?
    -->