programing

기수를 사용해야하는 이유는 무엇입니까?

nasanasas 2020. 10. 24. 10:22
반응형

기수를 사용해야하는 이유는 무엇입니까?


기수는 실제로 무엇을 의미합니까? 왜 필요합니까?

parseInt(10, radixValue); 

정수를 항상 밑이 10 인 숫자로 구문 분석하지 않을 수도 있으므로 기수를 제공하면 다른 숫자 체계를 지정할 수 있습니다.

기수는 단일 숫자 값의 수입니다. 16 진수는 16입니다. 8 진수는 8, 2 진수는 2, 등등 ...

에서 parseInt()기능을 제공하지 않고 당신이 기수 암시 할 수있는 몇 가지가있다. 사용자가 규칙 중 하나와 일치하지만 명시 적으로 의미하지는 않는 문자열을 입력하는 경우에도 이러한 문제가 발생할 수 있습니다. 예를 들면 :

// Numbers with a leading 0 used a radix of 8 (octal) before ECMAScript 5.
// These days, browsers will treat '0101' as decimal.
var result = parseInt('0101');

// Numbers that start with 0x use a radix of 16 (hexidecimal)
var result = parseInt('0x0101');

// Numbers starting with anything else assumes a radix of 10
var result = parseInt('101');

// Or you can specify the radix, in this case 2 (binary)
var result = parseInt('0101', 2);

같은 문자열 번호 0700가 있고 출력이 정수가되기를 원한다면 8 진수가 아닌 10 진수임을 700알려야 parseInt()합니다.

console.log(parseInt("0700"));
// 448

// I really wanted decimal (base 10)
console.log(parseInt("0700", 10));
// 700

// What is this? Binary, Decimal, Octal?
console.log(parseInt("0110"));
// 72

// as binary
console.log(parseInt("0110", 2));
// 6

참고 내가 절반 밖에 귀하의 질문에 대답했다. 기수가 실제로 무엇인지에 대한 좋은 정의는 다른 사람들을 참조하십시오.


기수는 계산 시스템의 기본입니다. 무한한 수의 숫자 체계가 있지만 대부분의 사람들에게 익숙한 것은 10 진법 (10 진수)과 2 진법 (이진법)입니다.

숫자 값은 염기에 따라 다르게 해석 될 수 있습니다. 예를 들어 2 진수 10은 10 진수 2로 표현할 수 있습니다.

의 경우 parseInt()기수를 사용하면 사용할 기준을 지정할 수 있습니다. 기본적으로 기수 10이 사용됩니다.

그러나 밑수 10을 사용하는 경우에도 기수는 항상 지정해야합니다.

parseInt("010") // 8을 반환합니다.

언뜻보기에 문이 10을 반환 할 것으로 예상 할 수 있습니다. 기수를 명시 적으로 사용하면 혼동을 피하는 데 도움이됩니다.

parseInt("010", 10) // 반환 : 10


기수는 숫자 체계의 기본 번호입니다. http://en.wikipedia.org/wiki/Radix

일반적으로 10과 다르기를 원하는 경우에만 기수를 지정하면됩니다.보다 구체적으로 ( http://www.w3schools.com/jsref/jsref_parseInt.asp에서 ) :

radix 매개 변수가 생략되면 JavaScript는 다음을 가정합니다.

문자열이 "0x"로 시작하면 기수는 16 (16 진수)입니다. 문자열이 "0"으로 시작하면 기수는 8 (8 진수)입니다. 이 기능은 더 이상 사용되지 않습니다. 문자열이 다른 값으로 시작하는 경우 기수는 10 (10 진수)입니다.


질문에 명확하게 답하는 추가 정보를 추가하기 만하면됩니다.

radix가 정의되지 않거나 0 (또는 부재)이면 JavaScript는 다음을 가정합니다.

  • [...]
  • If the input string begins with "0", radix is eight (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.
  • [...]

Source: MDN parseInt()


It's just my opinion but idea that "we need to use radix" quickly becomes outdated. The problem really was actual some time ago because people outside IT world usually don't use number notations other than decimal and quite often provide decimal numbers zero-padded like "010". But since ECMAScript 6 octal numbers in JS are prefixed by "0o" and not just "0" as it was in ECMAScript 5 and 3. So if you don't target IE family (that is not rare situation now) you can skip radix safely.


The radix parameter is used to specify which numeral system to be used, for example, a radix of 16 (hexadecimal) indicates that the number in the string should be parsed from a hexadecimal number to a decimal number.

If the radix parameter is omitted, JavaScript assumes the following:

If the string begins with "0x", the radix is 16 (hexadecimal)

If the string begins with "0", the radix is 8 (octal). This feature is deprecated

If the string begins with any other value, the radix is 10 (decimal)

Source W3Schools

참고URL : https://stackoverflow.com/questions/6611824/why-do-we-need-to-use-radix

반응형