Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the length of a number?

People also ask

What is length of a number?

The length of a number in base is the number of digits in the base- numeral for , given by the formula. where. is the floor function. The multiplicative persistence of an. -digit is sometimes also called its length.

How do you find the length of a number in Python?

The function len() is one of Python's built-in functions. It returns the length of an object. For example, it can return the number of items in a list. You can use the function with many different data types.

Do numbers have length JavaScript?

Use the toString() method to covert the Number to string, then the length() method gives you length.


var x = 1234567;

x.toString().length;

This process will also work forFloat Number and for Exponential number also.


Ok, so many answers, but this is a pure math one, just for the fun or for remembering that Math is Important:

var len = Math.ceil(Math.log(num + 1) / Math.LN10);

This actually gives the "length" of the number even if it's in exponential form. num is supposed to be a non negative integer here: if it's negative, take its absolute value and adjust the sign afterwards.

Update for ES2015

Now that Math.log10 is a thing, you can simply write

const len = Math.ceil(Math.log10(num + 1));

Could also use a template string:

const num = 123456
`${num}`.length // 6

You have to make the number to string in order to take length

var num = 123;

alert((num + "").length);

or

alert(num.toString().length);

I've been using this functionality in node.js, this is my fastest implementation so far:

var nLength = function(n) { 
    return (Math.log(Math.abs(n)+1) * 0.43429448190325176 | 0) + 1; 
}

It should handle positive and negative integers (also in exponential form) and should return the length of integer part in floats.

The following reference should provide some insight into the method: Weisstein, Eric W. "Number Length." From MathWorld--A Wolfram Web Resource.

I believe that some bitwise operation can replace the Math.abs, but jsperf shows that Math.abs works just fine in the majority of js engines.

Update: As noted in the comments, this solution has some issues :(

Update2 (workaround) : I believe that at some point precision issues kick in and the Math.log(...)*0.434... just behaves unexpectedly. However, if Internet Explorer or Mobile devices are not your cup of tea, you can replace this operation with the Math.log10 function. In Node.js I wrote a quick basic test with the function nLength = (n) => 1 + Math.log10(Math.abs(n) + 1) | 0; and with Math.log10 it worked as expected. Please note that Math.log10 is not universally supported.


You should go for the simplest one (stringLength), readability always beats speed. But if you care about speed here are some below.

Three different methods all with varying speed.

// 34ms
let weissteinLength = function(n) { 
    return (Math.log(Math.abs(n)+1) * 0.43429448190325176 | 0) + 1;
}

// 350ms
let stringLength = function(n) {
    return n.toString().length;
}

// 58ms
let mathLength = function(n) {
    return Math.ceil(Math.log(n + 1) / Math.LN10);
}

// Simple tests below if you care about performance.

let iterations = 1000000;
let maxSize = 10000;

// ------ Weisstein length.

console.log("Starting weissteinLength length.");
let startTime = Date.now();

for (let index = 0; index < iterations; index++) {
    weissteinLength(Math.random() * maxSize);
}

console.log("Ended weissteinLength length. Took : " + (Date.now() - startTime ) + "ms");


// ------- String length slowest.

console.log("Starting string length.");
startTime = Date.now();

for (let index = 0; index < iterations; index++) {
    stringLength(Math.random() * maxSize);
}

console.log("Ended string length. Took : " + (Date.now() - startTime ) + "ms");


// ------- Math length.

console.log("Starting math length.");
startTime = Date.now();

for (let index = 0; index < iterations; index++) {
    mathLength(Math.random() * maxSize);
}

There are three way to do it.

var num = 123;
alert(num.toString().length);

better performance one (best performance in ie11)

var num = 123;
alert((num + '').length);

Math (best performance in Chrome, firefox but slowest in ie11)

var num = 123
alert(Math.floor( Math.log(num) / Math.LN10 ) + 1)

there is a jspref here http://jsperf.com/fastest-way-to-get-the-first-in-a-number/2