테스트 사이트 - 개발 중인 베타 버전입니다

소수점 지원하는 Math.round

· 7년 전 · 2276

최근 고객의 의뢰요청으로 글로벌 사이트를 작업하면서 간단하게 함수 하나 만들었어요.

Math.round(100.3233) = 100 입니다.

소수점 자리를 지정해서 round 처리를 할 수 없어서 간단하게 하나 만들었네요. ^^

 

function Math_round(number, decimals){
    if( decimals == undefined ){
        decimals        = 0;
    }

    if( decimals == 0 ){
        return Math.round(number);
    }
    else{
        for( i = 0, x = 1 ; i < decimals ; i++ ){
            x        = x * 10;
        }
        return Math.round(number * x) / x;

    }
}

댓글 작성

댓글을 작성하시려면 로그인이 필요합니다.

로그인하기

게시글 목록

번호 제목
15969
jQuery .jquery
15968
15967
jQuery .is()
15966
15965
15964
15963
15962
jQuery .index()
15961
15960
15959
jQuery .html()
15958
15957
jQuery .hide ()
15956
15955
15954
15953
15952
15949
15947
15946
15945
15944
15942
15941
15940
15939
15938
15937
15936