Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- redux상태유지
- https://lo-victoria.com/introduction-to-redux-toolkit-for-beginners
- CSS
- for~in/for~of
- 내장고차함수
- JS#3일차달리자#초반인데#시간금방~
- variable#function
- react
- dom
- https://developer-talk.tistory.com/299
- ㄷㅌ
- Beesbeesbees
- children vs childrenNodes
- UI
- cmarket
- 자바스크립트#JS#slice#splice
- https://dasima.xyz/%EC%9E%90%EB%B0%94%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8-%EC%A0%9C%EA%B3%B1-math-pow-%EA%B3%84%EC%82%B0/
- 헷갈린다~
- removeCookie
- User Flow
- 노드교과서
- toString#String
- 자바스크립트#JS#var#let#const#undefined#null
- js
- https://www.daleseo.com/js-array-slice-splice/
- @redux-toolkit
- slice/splice/split
- UX
- 자바스크립트
- 자바스크립트#조건문#문자열
Archives
- Today
- Total
Daily Front_Minhhk
[JS] Number 타입을 String 타입으로 변환// toString(), String(), num+'',Template String ${(num)} 본문
Code개발일지
[JS] Number 타입을 String 타입으로 변환// toString(), String(), num+'',Template String ${(num)}
Minhhk 2022. 10. 25. 00:541. toString()으로 숫자를 문자열로 변환
toString() 메소드를 사용하여 숫자를 문자열로 변환할 수 있습니다.
const num = 5;
const str1 = num.toString();
const str2 = (100).toString();
const str3 = (-10.11).toString();
console.log(num + ', ' + typeof num);
console.log(str1 + ', ' + typeof str1);
console.log(str2 + ', ' + typeof str2);
console.log(str3 + ', ' + typeof str3);
Output:
5, number
5, string
100, string
-10.11, string
2. String()으로 숫자를 문자열로 변환
String() 메소드를 사용하여 숫자를 문자열로 변환할 수 있습니다.
const num = 5;
const str1 = String(num);
const str2 = String(100);
const str3 = String(-10.11);
console.log(num + ', ' + typeof num);
console.log(str1 + ', ' + typeof str1);
console.log(str2 + ', ' + typeof str2);
console.log(str3 + ', ' + typeof str3);
Output:
5, number
5, string
100, string
-10.11, string
3. 빈 문자열을 붙여서 숫자를 문자열로 변환
다음과 같이 Number 객체에 빈 문자열을 붙이면 String 객체로 변환됩니다.
const num = 5;
const str1 = num + '';
const str2 = 100 + '';
const str3 = -10.11 + '';
console.log(num + ', ' + typeof num);
console.log(str1 + ', ' + typeof str1);
console.log(str2 + ', ' + typeof str2);
console.log(str3 + ', ' + typeof str3);
Output:
5, number
5, string
100, string
-10.11, string
4. Template String으로 숫자를 문자열로 변환
ES6의 Template String을 사용하여 아래와 같이 Number를 문자열로 변환할 수 있습니다.
const num = 5;
const str1 = `${num}`;
const str2 = `${100}`;
const str3 = `${-10.11}`;
console.log(num + ', ' + typeof num);
console.log(str1 + ', ' + typeof str1);
console.log(str2 + ', ' + typeof str2);
console.log(str3 + ', ' + typeof str3);
Output:
5, number
5, string
100, string
-10.11, string
5. toFixed()로 숫자를 문자열로 변환
아래와 같이 toFixed()로 숫자를 문자열로 변환할 수 있습니다.
toFixed()는 toFixed(0)과 같으며, toFixed(n)은 소수점 n + 1 자리에서 반올림하고 n자리까지만 문자열로 변환합니다.
let num = 5.3;
const str1 = num.toFixed()
const str2 = (100.6666).toFixed()
const str3 = (100.1234).toFixed(1);
const str4 = (100.1234).toFixed(3);
console.log(num + ', ' + typeof num);
console.log(str1 + ', ' + typeof str1);
console.log(str2 + ', ' + typeof str2);
console.log(str3 + ', ' + typeof str3);
console.log(str4 + ', ' + typeof str4);
Output:
5.3, number
5, string
101, string
100.1, string
100.123, string
References
'Code개발일지' 카테고리의 다른 글
[JS] String 타입을 Number 변환 // +, -, parseInt(), Number(), parseFloat() (0) | 2022.10.25 |
---|---|
[JS] Math.pow() (0) | 2022.10.25 |
[JS] 배열의 slice()와 splice() 함수 (0) | 2022.10.24 |
[JS] 조건문, 문자열 (0) | 2022.10.24 |
[JS] undefined와 null의 차이점 / var let const 차이점 (0) | 2022.10.24 |