Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there ay JS function to find value before and after decimal point

I am validating a decimal number using JavaScript. Am just using NaN

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

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

Please dont suggest a substring!

like image 473
user987880 Avatar asked Oct 24 '11 20:10

user987880


1 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's suggestion is 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;
}

Usage

alert(a.before()); ///before
alert(a.after()); ///after
like image 177
John Hartsock Avatar answered Oct 20 '22 00:10

John Hartsock