반응형

자바스크립트 개발 환경 Microsoft Visual Studio 2015 버전 HTML 기능 및 Chrome을 이용하고 있습니다.





이번에는 Number 메서드에 대해 몇가지 알아보려 한다.


여러가지 메서드가 있는데 toString(), toFixed(), parseFloat(), parseInt()에 대해 알아보려 한다.


그 외 더 많은 내용들에 대해서는 링크를 통해 메서드를 제공하고자 한다.


Number Method Reference :: 

https://www.w3schools.com/jsref/jsref_obj_number.asp




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!DOCTYPE html>
<html>
    <head>
        <title>test title</title>
    </head>
 
    <body>
        <h2> JavaScript </h2>
 
        <p id="test0"></p>
        <p id="test1"></p>
        <p id="test2"></p>
 
        <p id="test3"></p>
        <p id="test4"></p>
        <p id="test5"></p>
        <p id="test6"></p>
 
        <script>
            var a = 0.1;
            var b = 0.2;
 
            // string 리턴
            document.getElementById('test0').innerHTML = 'toString() Method';
            document.getElementById('test1').innerHTML = (a + b).toString();
            document.getElementById('test2').innerHTML = (a * b).toString();
 
 
            // string 리턴, 인자는 소수 몇째자리 까지 나타내는지 의미
            a = 9.56;
            document.getElementById('test3').innerHTML = 'toFixed() Method';
            document.getElementById('test4').innerHTML = a.toFixed(0);
            document.getElementById('test5').innerHTML = a.toFixed(1);
            document.getElementById('test6').innerHTML = a.toFixed(6);
            
        </script>
 
    </body>
</html>
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus


클릭하시면 결과 화면을 확인 할 수 있습니다.



위의 toString()메서드는 Number 타입을 String 타입으로 변경시켜주는 역할을 한다.


그리고 아래 toFixed()메서드는 소수점 몇째자리까지 나타낼 지, 반올림을 통해 나타낸다.





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<!DOCTYPE html>
<html>
    <head>
        <title>test title</title>
    </head>
 
    <body>
        <h2> JavaScript </h2>
 
        <p id="test0"></p>
        <p id="test1"></p>
        <p id="test2"></p>
 
        <p id="test3"></p>
        <p id="test4"></p>
        <p id="test5"></p>
        <p id="test6"></p>
 
        <script>
            // string 리턴
            document.getElementById('test0').innerHTML = 'Number() Method';
            document.getElementById('test1').innerHTML = parseInt("10.56");
            document.getElementById('test2').innerHTML = parseInt("10 ten");
 
            // string 리턴, 인자는 소수 몇째자리 까지 나타내는지 의미
            document.getElementById('test3').innerHTML = 'parseInt() Method';
            document.getElementById('test4').innerHTML = parseFloat("10");
            document.getElementById('test5').innerHTML = parseFloat("10.33");
            document.getElementById('test6').innerHTML = parseFloat("ten 10.33");
            
        </script>
 
    </body>
</html>
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus



클릭하시면 결과 화면을 확인 할 수 있습니다.



위의 parseInt는 수를 정수로 바꿔주는 역할을 하게되고, "10 ten"처럼 저렇게 값이 들어오면 첫번째 값만 해석하게 된다.


예를들어 "ten 10"이 들어오면 NaN이 출력된다.



두번째 parseFloat는 수를 소수로 바꾸게 된다.


"ten 10.1"같은 경우는 위 parseInt와 동일하다.



Number Properties

PropertyDescription
MAX_VALUEReturns the largest number possible in JavaScript
MIN_VALUEReturns the smallest number possible in JavaScript
NEGATIVE_INFINITYRepresents negative infinity (returned on overflow)
NaNRepresents a "Not-a-Number" value
POSITIVE_INFINITYRepresents infinity (returned on overflow)


위 내용은 매크로 이미 지정된 값이다.


예를들어 var a = Number.MAX_VALUE를 하면 자바스크립트에서 쓸 수 있는 최댓값을 저장한다.































반응형