By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,454 Members | 3,103 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
9 Replies


acoder
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
  1. String.prototype.contains = function(t) { 
  2. return this.indexOf(t) >= 0 ? true : false ;
  3. };
  4. var x=12.34;
  5. function valid()
  6. {
  7.      if(x.contains('.'))
  8.     {
  9.          alert('please enter integer only');
  10.          return false;
  11.       }
  12. }
Jun 25 '08 #3

hsriat
Expert 100+
P: 1,654
hey man
here is the solution

Expand|Select|Wrap|Line Numbers
  1. String.prototype.contains = function(t) { 
  2. return this.indexOf(t) >= 0 ? true : false ;
  3. };
  4. var x=12.34;
  5. function valid()
  6. {
  7.      if(x.contains('.'))
  8.     {
  9.          alert('please enter integer only');
  10.          return false;
  11.       }
  12. }
I don't see any need of such a function when JS has inbuilt function like parseInt and match.

Expand|Select|Wrap|Line Numbers
  1. if (input != parseInt(input))
  2. alert('Hey dude, I told ya to enter integer only! Can\'t you read?');
OR
Expand|Select|Wrap|Line Numbers
  1. if (!input.match(/^[0-9]+$/))
  2. alert('huh!.. enter an integer value, you.. ');
PS: OP can use parseFloat to check if it a floating value.
Jun 25 '08 #4

gits
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
  1. var x = 'foo.bar';
kind regards
Jun 25 '08 #5

hsriat
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
  1. var x = 'foo.bar';
kind regards
Apart from that, if input is "foo", it would get validated.
Jun 25 '08 #6

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

hsriat
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

hsriat
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

Post your reply

Sign in to post your reply or Sign up for a free account.