JS) parseInt()

Ceciliaยท2022๋…„ 12์›” 20์ผ
0

JavaScript

๋ชฉ๋ก ๋ณด๊ธฐ
23/36
post-thumbnail

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/parseInt

https://ko.javascript.info/number#ref-13




parseInt()


parseInt() ํ•จ์ˆ˜๋Š” ๋ฌธ์ž์—ด ์ธ์ž๋ฅผ ํŒŒ์‹ฑํ•˜์—ฌ ํŠน์ • ์ง„์ˆ˜(์ˆ˜์˜ ์ง„๋ฒ• ์ฒด๊ณ„์—์„œ ๊ธฐ์ค€์ด ๋˜๋Š” ๊ฐ’)์˜ ์ •์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

function roughScale(x, base) {
  const parsed = parseInt(x, base);
  if (isNaN(parsed)) { return 0; }
  return parsed * 100;
}

console.log(roughScale(' 0xF', 16));
// expected output: 1500

console.log(roughScale('321', 2));
// expected output: 0
alert( parseInt('100px') ); // 100
alert( parseFloat('12.5em') ); // 12.5

alert( parseInt('12.3') ); // 12, ์ •์ˆ˜ ๋ถ€๋ถ„๋งŒ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.
alert( parseFloat('12.3.4') ); // 12.3, ๋‘ ๋ฒˆ์งธ ์ ์—์„œ ์ˆซ์ž ์ฝ๊ธฐ๋ฅผ ๋ฉˆ์ถฅ๋‹ˆ๋‹ค.



number x.xxxxxxxxxxx๋ฅผ ์ •์ˆ˜ x๋กœ ๋ฐ˜ํ™˜!

profile
'์ด๊ฒŒ ์ตœ์„ ์ผ๊นŒ?'๋ผ๋Š” ๊ณ ์ฐฐ์„ ํ†ตํ•ด ๋์—†์ด ์„ฑ์žฅํ•˜๊ณ , ๊ทธ ๊ณผ์ •์„ ์ฆ๊ธฐ๋Š” ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž์˜ ์ž…์žฅ์„ ์ƒ๊ฐํ•˜๋ฉฐ ์ตœ์„ ์˜ ํŽธ์˜์„ฑ์„ ์ฐพ๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€