Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way of getting the number of decimals in a number in JavaScript [duplicate]

Is there a better way of figuring out the number of decimals on a number than in my example?

var nbr = 37.435.45; var decimals = (nbr!=Math.floor(nbr))?(nbr.toString()).split('.')[1].length:0; 

By better I mean faster to execute and/or using a native JavaScript function, ie. something like nbr.getDecimals().

Thanks in advance!

EDIT:

After modifying series0ne answer, the fastest way I could manage is:

var val = 37.435345; var countDecimals = function(value) {     if (Math.floor(value) !== value)         return value.toString().split(".")[1].length || 0;     return 0; } countDecimals(val); 

Speed test: http://jsperf.com/checkdecimals

like image 588
PhilTrep Avatar asked Jun 28 '13 16:06

PhilTrep


People also ask

How do I get 2 decimal places in JavaScript?

Use the toFixed() method to format a number to 2 decimal places, e.g. num. toFixed(2) . The toFixed method takes a parameter, representing how many digits should appear after the decimal and returns the result.

How do you find the number of decimal places?

The first digit after the decimal represents the tenths place. The next digit after the decimal represents the hundredths place. The remaining digits continue to fill in the place values until there are no digits left. The number.

How do I limit the number of decimal places in JavaScript?

To limit decimal places in JavaScript, use the toFixed() method by specifying the number of decimal places. This method: Rounds the number. Converts it into a string.

What is the use of toFixed 2 in JavaScript?

The toFixed() method converts a number to a string. The toFixed() method rounds the string to a specified number of decimals.


1 Answers

Number.prototype.countDecimals = function () {     if(Math.floor(this.valueOf()) === this.valueOf()) return 0;     return this.toString().split(".")[1].length || 0;  } 

When bound to the prototype, this allows you to get the decimal count (countDecimals();) directly from a number variable.

E.G.

var x = 23.453453453; x.countDecimals(); // 9 

It works by converting the number to a string, splitting at the . and returning the last part of the array, or 0 if the last part of the array is undefined (which will occur if there was no decimal point).

If you do not want to bind this to the prototype, you can just use this:

var countDecimals = function (value) {     if(Math.floor(value) === value) return 0;     return value.toString().split(".")[1].length || 0;  } 

EDIT by Black:

I have fixed the method, to also make it work with smaller numbers like 0.000000001

Number.prototype.countDecimals = function () {      if (Math.floor(this.valueOf()) === this.valueOf()) return 0;      var str = this.toString();     if (str.indexOf(".") !== -1 && str.indexOf("-") !== -1) {         return str.split("-")[1] || 0;     } else if (str.indexOf(".") !== -1) {         return str.split(".")[1].length || 0;     }     return str.split("-")[1] || 0; }   var x = 23.453453453; console.log(x.countDecimals()); // 9  var x = 0.0000000001; console.log(x.countDecimals()); // 10  var x = 0.000000000000270; console.log(x.countDecimals()); // 13  var x = 101;  // Integer number console.log(x.countDecimals()); // 0
like image 58
Matthew Layton Avatar answered Sep 23 '22 14:09

Matthew Layton