Dog Feet Developer/JavaScript

[JavaScript/JQuery] 컨트롤소수점올림버림반올림 과 그외 처리

티엔느 2020. 7. 20. 10:45
반응형

소수점 올림

document.write(Math.ceil(123.456)+"\n"); //124

document.write(Math.ceil(123.567)+"\n"); //124

 

소수점 버림

document.write(Math.floor(123.456)+"\n"); //124

document.write(Math.floor(123.567)+"\n"); //124

 

소수점 반올림 document.write(Math.round(123.456)+"\n"); //123

document.write(Math.round(123.567)+"\n"); //124

 

toFixed : 숫자를 문자열로 변환하면서 지정된 소수점 이하 숫자를 반올림하여 출력

document.write((123.456).toFixed(0)+"\n"); //123

document.write((123.456).toFixed(2)+"\n"); //123.46

document.write((123.456).toFixed(4)+"\n"); //123.4560

document.write((123.789).toFixed(0)+"\n"); //124

document.write((123.789).toFixed(2)+"\n"); //123.79

document.write((123.789).toFixed(4)+"\n"); //123.7890

 

toExponential : 숫자를 문자열로 변환하면서 소수점 앞의 숫자 하나와 지정된 개수의 소수점 이후 숫자로 구성되는 지수표기법을 사용하여 출력

document.write((123.456).toExponential(0)+"\n"); //1e+2

document.write((123.456).toExponential(2)+"\n"); //1.23e+2

document.write((123.456).toExponential(4)+"\n"); //1.2346e+2

document.write((1234.56).toExponential(0)+"\n"); //1e+3

document.write((1234.56).toExponential(2)+"\n"); //1.23e+3

document.write((1234.56).toExponential(4)+"\n"); //1.2346e+3

 

toPrecision : 지정된 수의 유효 숫자 개수만큼 숫자로 출력. 만약 유효 숫자 갯수가 숫자의 정수부분 전체를 출력할 만큼 충분하지 않다면 지수 표기법으로 출력

 

document.write((123.456).toExponential(2)+"\n"); //1.23e+2

document.write((123.456).toExponential(4)+"\n"); //1.2346e+2

document.write((123.456).toExponential(7)+"\n"); //1.2345600e+2

반응형