Is there a JS function to search for values โ€‹โ€‹before and after the decimal point

I am checking a decimal number using JavaScript. Just use NaN

var a = 12345.67 Is there any javascript function to get the counter or value before and after the decimal point.

 before() should return 1234 after() should return 67 

Please do not offer a substring!

+6
source share
6 answers
 var a = 12345.67; alert(a.toString().split(".")[0]); ///before alert(a.toString().split(".")[1]); ///after 

Here is a simple fiddle http://jsfiddle.net/qWtSc/


zzzzBov offers this

 Number.prototype.before = function () { var value = parseInt(this.toString().split(".")[0], 10);//before return value ? value : 0; } Number.prototype.after = function () { var value = parseInt(this.toString().split(".")[1], 10);//after return value ? value : 0; } 

Using

 alert(a.before()); ///before alert(a.after()); ///after 
+19
source

before easy. This is just a rounding operation.

 var before = function(n) { return Math.floor(n); }; 

after harder without string handling. I mean, how would you deal with after(Math.PI) ? You cannot hold an integer with an infinite number of digits.

But with some string processing, it's simple, just know that it will not be exactly because of the wonders of floating point math.

 var after = function(n) { var fraction = n.toString().split('.')[1]; return parseInt(fraction, 10); }; 
+7
source
 var decimalPlaces = 2; var num = 12345.673 var roundedDecimal = num.toFixed(decimalPlaces); var intPart = Math.floor(roundedDecimal); var fracPart = parseInt((roundedDecimal - intPart), 10); //or var fractPart = (roundedDecimal - intPart) * Math.pow(10, decimalPlaces); 
+2
source

Play other answers ... and you need a digital version. Even easier is to convert it to a string and disable the split function ...

 function getNatural(num) { return parseFloat(num.toString().split(".")[0]); } function getDecimal(num) { return parseFloat(num.toString().split(".")[1]); } var a = 12345.67; alert(getNatural(a)); ///before alert(getDecimal(a)); ///after 

http://jsfiddle.net/rlemon/qWtSc/1/

+2
source

Unfortunately, there is no way to get the fractional part in a reliable way using mathematical functions, since quite odd rounding often occurs, depending on the Javascript mechanism used. It is best to do this to convert it to a string, and then check if the results are in decimal or scientific notation.

 Number.prototype.after = function() { var string = this.toString(); var epos = string.indexOf("e"); if (epos === -1) { // Decimal notation var i = string.indexOf("."); return i === -1 ? "" : n.substring(i + 1); } // Scientific notation var exp = string.substring(epos + 1) - 0; // this is actually faster // than parseInt in many browsers var mantix = n.string.substring(0, epos).replace(".", ""); if (exp >= -1) return mantix.substring(exp + 1); for (; exp < -1; exp++) mantix = "0" + mantix; return mantix; } 
0
source

To find the counter / character length after the dot:

 var a = 12345.67; var after_dot = (a.toString().split(".")[1]).length; var before_dot= (a.toString().split(".")[0]).length; 
0
source

Source: https://habr.com/ru/post/899993/


All Articles