[Today I Learned] 12월 2주차 day4

suwoncityboyyy·2022년 12월 19일
0

데이터타입 관련 메서드의 모든 것 (문제 풀 때 기억안나는거 찾기 위해서 기록함)

parseInt, toFixed

const pi = 3.14159265358979;
console.log(pi);

const str = pi.toFixed(2);  // 소수점 두번째자리까지만 출력하고 나머지는 출력x , 문자열로 변환
console.log(str);        // 문자 ' 3.14 ' 출력
console.log(typeof str);     // 데이터타입 string 출력 

const integer = parseInt(str);    // 정수로 변환해서 integer라는 변수에 할당
const float = parseFloat(str);    // 실수로 변환해서 float 라는 변수에 할당
console.log(integer);            // 정수 3 출력
console.log(float);              // 실수 3.14 출력
console.log(typeof integer , typeof float);      // 데이터타입 number , number 출력

Math함수

console.log('abs :', Math.abs(-12));             // 절대값 12 출력

console.log('min :', Math.min(2,8));             // 최소값 2 출력

console.log('max :', Math.max(2,8));           // 최대값 8 출력

console.log('ceil: ', Math.ceil(3.14));        // 4 출력 (올림)

console.log('floor : ', Math.floor(3.14));       // 3 출력 (내림)

console.log('round : ', Math.round(3.14));       // 3 출력 (반올림)

빈문자열을 붙여서 숫자를 문자열로 변환

const num = 5;

const str1 = num + '';
const str2 = 100 + '';
const str3 = -10.11 + '';

console.log(num + ', ' + typeof num);  // 5, number
console.log(str1 + ', ' + typeof str1);  // 5, string
console.log(str2 + ', ' + typeof str2);  // 100, string
console.log(str3 + ', ' + typeof str3);   // -10.11, string
profile
주니어 개발자 기술노트

0개의 댓글