435,454 Members | 3,103 Online
Need help? Post your question and get tips & solutions from a community of 435,454 IT Pros & Developers. It's quick & easy.

# to check float values in javascript, any fun??

 P: 1 Tell me Guys , If there is any Fun to check float numbers in javascript ?? Tell me d solution Thanks Amit Feb 2 '08 #1
9 Replies

 Expert Mod 15k+ P: 16,027 parseFloat will convert a string into a floating point number. Feb 2 '08 #2

 P: 1 hey man here is the solution Expand|Select|Wrap|Line Numbers String.prototype.contains = function(t) {  return this.indexOf(t) >= 0 ? true : false ; }; var x=12.34; function valid() {      if(x.contains('.'))     {          alert('please enter integer only');          return false;       } } Jun 25 '08 #3

 Expert 100+ P: 1,654 hey man here is the solution Expand|Select|Wrap|Line Numbers String.prototype.contains = function(t) {  return this.indexOf(t) >= 0 ? true : false ; }; var x=12.34; function valid() {      if(x.contains('.'))     {          alert('please enter integer only');          return false;       } } I don't see any need of such a function when JS has inbuilt function like parseInt and match. Expand|Select|Wrap|Line Numbers if (input != parseInt(input)) alert('Hey dude, I told ya to enter integer only! Can\'t you read?'); OR Expand|Select|Wrap|Line Numbers if (!input.match(/^[0-9]+\$/)) alert('huh!.. enter an integer value, you.. '); PS: OP can use parseFloat to check if it a floating value. Jun 25 '08 #4

 Expert Mod 5K+ P: 5,333 as far as i could see the shown contains() method would even return true when it would check: Expand|Select|Wrap|Line Numbers var x = 'foo.bar'; kind regards Jun 25 '08 #5

 Expert 100+ P: 1,654 as far as i could see the shown contains() method would even return true when it would check: Expand|Select|Wrap|Line Numbers var x = 'foo.bar'; kind regards Apart from that, if input is "foo", it would get validated. Jun 25 '08 #6

 Expert Mod 15k+ P: 16,027 One more problem: x is not a string. Jun 25 '08 #7

 Expert 100+ P: 1,654 aww.... we guys are so bad. We should welcome him instead of doing all this. Hey Rajendar, welcome to bytes! Keep posting mate. :) Jun 25 '08 #8

 10K+ P: 13,264 aww.... we guys are so bad. We should welcome him instead of doing all this. Hey Rajendar, welcome to bytes! Keep posting mate. :) You started it. Jun 25 '08 #9

 Expert 100+ P: 1,654 You started it. My apologies... I'll rephrase it... :) I am such a bad guy. I should welcome him instead of starting all this. Regards, Harpreet Jun 25 '08 #10