인터넷정보

JS 간단한 소수점 계산 스크립트

알 수 없는 사용자 2007. 11. 28. 15:55
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>real_multiple</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />

<style type="text/css">
.title {float:left ; width:150px}
</style>

<script type="text/javascript">
function real_multiple() {
        var result = 1;
        var log = 0;
        for(var i=0,len=arguments.length ; i<len ; i++) {
                var number = parseFloat(arguments[i]);
                var string = number.toString(10);
                var place = string.indexOf(".");
                place = (place==-1)?0:(string.length-place-1);
                result *= (number * Math.pow(10,place));
                log += place;
        }
        return result / Math.pow(10,log);
}

function test() {
        var n_1 = document.getElementById("n_1").value;
        var n_2 = document.getElementById("n_2").value;
        var f_1 = parseFloat(n_1);
        var f_2 = parseFloat(n_2);

        document.getElementById("s_1").innerHTML = f_1 * f_2;
        document.getElementById("s_2").innerHTML = real_multiple(n_1,n_2);
}

</script>

</head>

<body>
<input type="text" id="n_1" value="10.4233" /> * <input type="text" id="n_2" value="10.423" /> = <input type="button" value="계산" onclick="test()" />
<p><span class="title">js default calculation</span> : <span id="s_1"></span></p>
<p><span class="title">real_multiple</span> : <span id="s_2"></span></p>
</body>
</html>

출처:http://www.seeker.kr
반응형