JavaScript: int to string

0

each built-in JavaScript object kind has a toString() way. This manner is robotically referred to as when the article needs to be represented as a string price (i.e., whilst you concatenate some string price with a bunch), or it can be referred to as manually when the sort of kind conversion is wanted. The toString() way works on all the same old JS objects: arrays, dates, numbers, errors, et cetera. For quantity types, the toString() means can even take an non-compulsory “radix” argument: number.toString(radix) The radix (or base) is the whole selection of distinctive digits, including zero, that are used to symbolize a particular number system. For number programs as opposed to the decimal machine, you need to use the radix argument to appropriately stringify a number from every other quantity gadget, such because the octal (base eight) and hexadecimal (base sixteen) number programs. here are some examples of the toString() method getting used on numbers: 1 2 three four 5 6 7 eight 9 10 eleven 12 13 14 15 sixteen 17 18 19 var x = 10; // integer number var y = 7.950; // floating point number var z = 1.21e–6; // floating level number in a scientific notation var inf = number.POSITIVE_INFINITY; // infinity   console.log(‘hi there’ + x); // ‘hello10’ – toString() is called automatically console.log(x.toString()); // “10” – decimal base console.log(x.toString(2)); // “1010” – binary base console.log(x.toString(8)); // “12” – octal base console.log(x.toString(sixteen)); // “a” – hex base   console.log(y.toString()); // “7.ninety five” – be aware the stripped “0” at the finish console.log(y.toString(2)); // “111.11110011001100111000011100010110000010010101011011” console.log(y.toString(sixteen)); // “7.f33387160956c”   console.log(z.toString()); // “0.00000121”   console.log(inf.toString()); // “Infinity” console.log(inf.toString(2)); // “Infinity” There isn’t in point of fact far more to the usage of toString() on numbers in JavaScript. As at all times, in case you have any questions, ask away in a comment under.

Share.

Leave A Reply