实现说明
顶层就这几处状态:value、boxed。
使用要点
- 每一步打印都带着自己的标签:
binary :、base36 :、negative:。
常见问题
复制和运行之间有安装步骤吗?
没有。从复制到运行之间没有别的要做。 状态都挂在模块顶层:value、boxed。
核心语句
const boxed = new Number(255);
const value = 255;
代码示例
// toString(radix) 输出 2 到 36 任意进制的表示
const value = 255;
console.log('hex :', value.toString(16));
console.log('octal :', value.toString(8));
console.log('binary :', value.toString(2));
console.log('base36 :', (123456789).toString(36));
// parseInt 用同一进制解析回十进制,往返无损
console.log('back :', parseInt(value.toString(16), 16) === value);
// 小数用 radix 也能表示
console.log('frac :', (0.5).toString(2));
// 负数保留符号,radix 作用于绝对值
console.log('negative:', (-42).toString(16));
// 无参默认十进制;包装对象取原始值后再转
const boxed = new Number(255);
console.log('boxed :', boxed.valueOf().toString(16) === boxed.toString(16));