본문 바로가기
개발일지/JavaScript

JS 계산 / 날짜 / 문자 메서드

by 한삐 2022. 8. 9.
728x90

계산 / 날짜 / 문자 메서드 일부 정리

 

계산식
Math.pow(n,m) : n의 m제곱
올림(Math.ceil(n))
내림(Math.floor(n))
반올림(Math.round(n))
소수점 반올림 .toFixed(n) - 소수점 n의자릿수까지 표현 / 매개변수가 없으면 1의자릿수 반환

 

let num = 10.987654321

num.toFixed() // 11
num.toFixed(1) // 11.0
num.toFixed(2) // 10.99
num.toFixed(3) // 10.988
num.toFixed(4) // 10.9877
num.toFixed(5) // 10.98765
num.toFixed(10) // 10.9876543210
num.toFixed(14) // 10.98765432100000

 

날짜 계산
getDay() - 주어진 날짜의 현지 시간 기준 요일을 반환

// Sunday - Saturday : 0 - 6
const someday= new Date('2022-01-23'); 
// 날짜 형식은 자유로움 ('january 23 ,2022') 처럼 사용 가능
const day1 = someday.getDay();
console.log(day1);  //  22.01.23은 일요일이므로, 0 반환

 

문자열

string.charAt(0) - 스트링의 0번째 글자 가져오기(result = string)
string.substring(0,5) - 스트링의 0~4번째 글자 가져오기(5를 포함하지 않음)

문자열을 숫자로 변환

Number(s)
parseInt(s)
parseFloat(s)

 

기타

n.toString() 숫자를 스트링으로 변환

String.includes()는 문자열이 특정 문자열을 포함하는지 확인

let str1 = "12345"
str1.includes(1) // true
728x90

'개발일지 > JavaScript' 카테고리의 다른 글

javaScript 배열 다뤄보기 1  (0) 2022.08.23
시간상 사각지대 TDZ  (0) 2022.08.16
js 반복문 다뤄보기  (0) 2022.08.09
JavaScript 숫자열/문자열 다뤄보기  (0) 2022.08.09
VSC에 JS 콘솔 찍기  (0) 2022.08.07

댓글