最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to convert number start with 0 to string equivalent of the value? - Stack Overflow

programmeradmin0浏览0评论

I want to convert a number start with 0 to string equivalent of the value.

If I run

var num = 12;
var int = num.toString();
console.log(int);

it logs 12 as expected but if I apply the toString() to a number start with 0 like,

var num = 012;
var int = num.toString();
console.log(int);

it logs 10, why?

I want to convert a number start with 0 to string equivalent of the value.

If I run

var num = 12;
var int = num.toString();
console.log(int);

it logs 12 as expected but if I apply the toString() to a number start with 0 like,

var num = 012;
var int = num.toString();
console.log(int);

it logs 10, why?

Share Improve this question edited Sep 22, 2019 at 9:32 georg 215k56 gold badges322 silver badges399 bronze badges asked Sep 22, 2019 at 9:09 Rehab AliRehab Ali 974 bronze badges 6
  • 5 Because if a number starts with 0 it is interpreted in base 8 – Federico klez Culloca Commented Sep 22, 2019 at 9:16
  • Now, if you want to convert it, since it's in the code and not input by the user, simply don't put that 0 before the number. – Federico klez Culloca Commented Sep 22, 2019 at 9:18
  • Not to detract from the answers, but if the simple fact that the number is octal was really what was being asked, there are a ton of duplicates. If the question is "how to convert an octal number to a decimal representation as if it wasn't octal", then that's a question worth answering. Can OP please clarify? – Federico klez Culloca Commented Sep 22, 2019 at 9:21
  • Also, try to develop your scripts in the "strict mode", which disallows some oddities of javascript, including this. – georg Commented Sep 22, 2019 at 9:24
  • 1 If you need to ask a new question, do that. Please don't change your question to a pletely new one. – georg Commented Sep 22, 2019 at 9:33
 |  Show 1 more ment

3 Answers 3

Reset to default 5

Number starting with 0 is interpreted as octal (base-8).

In sloppy mode (the default) numbers starting with 0 are interpreted as being written in octal (base 8) instead of decimal (base 10). If has been like that from the first released version of Javascript, and has this syntax in mon with other programming languages. It is confusing, and have lead to many hard to detect buggs.

You can enable strict mode by adding "use strict" as the first non-ment in your script or function. It removes some of the quirks. It is still possible to write octal numbers in strict mode, but you have to use the same scheme as with hexadecimal and binary: 0o20 is the octal representation of 16 decimal.

The same problem can be found with the function paseInt, that takes up to two parameters, where the second is the radix. If not specified, numbers starting with 0 will be treated as octal up to ECMAScript 5, where it was changed to decimal. So if you use parseInt, specify the radix to be sure that you get what you expected.

"use strict";

// Diffrent ways to write the same number:
const values = [
  0b10000, // binary
  0o20, // octal
  16, // decimal,
  0x10 // hexadecimal
];

console.log("As binary:", values.map( value => value.toString(2)).join());
console.log("As decimal:", values.join());
console.log("As ocal", values.map( value => value.toString(8)).join());
console.log("As hexadecimal:", values.map( value => value.toString(16)).join());
console.log("As base36:", values.map( value => value.toString(36)).join());

All you have to do is add String to the front of the number that is

var num = 12;
var int = String(num);
console.log(int);

And if you want it to look like this 0012 all you have to do is

var num = 12;
var int = String(num).padStart(4, '0');
console.log(int);
发布评论

评论列表(0)

  1. 暂无评论