• 회원가입
  • 로그인
  • 구글아이디로 로그인

[Number] JS - toPrecision() 메서드 - 숫자를 지정 길이로 변환 (= 투프리시전)

 

toPrecision() 예제

 

<button onclick="homzznag()">클릭</button>


<p id="demo"></p>


<script>

function homzznag() {

  var num = 12.345;

  document.getElementById("demo").innerHTML = num.toPrecision(2);

}

</script>

 

결과보기

결과값: 12

 

toPrecision() 정의

 

숫자를 지정 길이로 변환

 


 

1.

지정 길이를 만들기 위해 소수점과 null (= 자릿수 표시 위한 0) 추가되기도 함.

2.

모든 브라우저 지원

3.
ECMAScript 3

 

 

toPrecision() 구문

 

number.toPrecision(n)

 


 

[매개변수]

n

선택. 자릿수 (= 지정 길이)
생략된 경우, 숫자 자체 반환.

 

 

toPrecision() 예제

 

<button onclick="homzznag()">클릭</button>


<p id="demo"></p>


<script>

function homzznag() {

  var num = 12.345;

  var a = num.toPrecision(); // 12.345

  var b = num.toPrecision(2); // 12

  var c = num.toPrecision(4); // 12.34

  var d = num.toPrecision(6); // 12.3450


  var n = a + "<br>" + b + "<br>" + c + "<br>" + d;

 

  document.getElementById("demo").innerHTML = n;

}

</script>

 

결과보기



분류 제목
Operator JS - typeof 연산자 - 데이터타입 반환/확인/체크 (= typeof연산자 = 타입어브연산자/타입어브…
Operator JS - delete 연산자 (delete Operator = 삭제연산자)
Operator JS - in 연산자 (in Operator = 포함연산자)
Operator JS - instanceof 연산자 (instanceof Operator = 인스턴스연산자)
Operator JS - void 연산자 (= void Operator = 보이드 연산자) + 클릭 배경색 변경
Statement JS - 구문 (Statements)
Statement JS - break 구문 ★ (= break문 = 브레이크문)
Statement JS - continue 구문 ★ (= continue문 = 컨티뉴문)
Statement JS - debugger 구문 (= debugger문 = 디버거문)
Statement JS - do ... while 구문 (= 두와일구문 = 두와일문 = 두와일반복문)
Statement JS - for 구문 (= for문 = 포구문 = 포문 = 포반복문)
Statement JS - for...in 구문 (= 포인구문 = 포인문 = 포인반복문)
Statement JS - function 구문 (= 함수구문)
Statement JS - if ... else if ... else ... 구문 (= if문 = 이프문 = 이프조건문)
Statement JS - return 구문 ★★ (= return문 = return구문 = 리턴문 = 반환문 = 리턴구문)…
Statement JS - switch 조건문 ★ (= switch문 = 스위치문)
Statement JS - throw 구문 (= 쓰로우문 = 쓰로우구문)
Statement JS - try ... catch ... finally 구문 (= 트라이캐치파이널구문)
Statement JS - var 구문 (= 변수정의)
Statement JS - while 구문 (= 와일문 = 와일반복문)
9/67
목록
찾아주셔서 감사합니다. Since 2012