금액 대소 문자 변환 (JS + 정규)

1893 단어 대소 문자

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function test(cash) {
	var num = cash;
	// cash    0           num,   cash    0     ,            
	for (var i=0; i < cash.length; i++){
		if (cash.substr(i,1) == 0)
			num = cash.substring(i+1);
		else
			break;
	}
	var strOutput = "";
	var strUnit = '              ';
	num += "00";
	var intPos = num.indexOf('.');
	if (intPos >= 0)
		//  num  '.',       00   
		num = num.substring(0, intPos) + num.substr(intPos + 1, 2);
	if(num.length > strUnit.length){
		return "         ,    ";
	}
	//strUnit    num              
	strUnit = strUnit.substr(strUnit.length - num.length);
	//                  ,    strUnit    ,,      replace     $1,             ,    Perl   =~ s /(replace code)/$1/   ,    Perl       
	for (var i=0; i < num.length; i++)
		strOutput += '          '.substr(num.substr(i,1),1) + strUnit.substr(i,1);
	return strOutput.replace(/ [     ]/g, ' ').replace(/ {2,}/g, ' ').replace(/ ([   ])/g, '$1').replace(/  /, ' ').replace(/([  ]) /, '$1 ').replace(/^([   ])/, "  ");
};
</script>
</head>
<body>
<input type="button" name="test1" value="Test"
	onclick="alert(test('999010256300.30'));"></input>
</body>
</html>

좋은 웹페이지 즐겨찾기