为什么在调用parseInt时需要使用基数参数? [英] Why do we need to use radix parameter when calling parseInt?

查看:73
本文介绍了为什么在调用parseInt时需要使用基数参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基数实际上是什么意思?我们为什么需要它?

What does radix actually means? Why do we need it?

parseInt(10, radixValue); 

推荐答案

您可能并不总是希望将整数解析为以10为底的数字,因此提供基数可以指定其他数字系统.

You might not always want to parse the integer into a base 10 number, so supplying the radix allows you to specify other number systems.

基数是一位数字的值数.十六进制为16.八进制为8,二进制为2,依此类推...

The radix is the number of values for a single digit. Hexidecimal would be 16. Octal would be 8, Binary would be 2, and so on...

parseInt()函数中,您可以执行一些操作以提示不提供基数.如果用户输入的字符串与规则之一匹配,但没有明确规定,则这些方法也可能对您不利.例如:

In the parseInt() function, there are several things you can do to hint at the radix without supplying it. These can also work against you if the user is entering a string that matches one of the rules but doesn't expressly mean to. For example:

// 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);

这篇关于为什么在调用parseInt时需要使用基数参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆