473,385 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

to check float values in javascript, any fun??

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 12239
acoder
16,027 Expert Mod 8TB
parseFloat will convert a string into a floating point number.
Feb 2 '08 #2
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
1,654 Expert 1GB
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
5,390 Expert Mod 4TB
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
1,654 Expert 1GB
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
16,027 Expert Mod 8TB
One more problem: x is not a string.
Jun 25 '08 #7
hsriat
1,654 Expert 1GB
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
r035198x
13,262 8TB
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
1,654 Expert 1GB
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

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

Similar topics

5
by: Lars Plessmann | last post by:
I have a problem with typecast methods. Here is a setter of an object: function setKind($kind) { if (is_int((int)$kind) and (strlen($kind)<=6)) { $this->kind = $kind; return true;
1
by: GIMME | last post by:
If I have : <input name="a1" value="1"> <input name="a2" value="3"> <input name="a3" value="2"> And I want the user to enter the values 1,2, and 3 but not necessarily in that order .... (The...
4
by: Georg Vassilopulos | last post by:
Hello! How do I check user input for following formats: I only want to accept floats or integers written as: 232,45 232 242.45 345
4
by: Lodewijk Smit | last post by:
Hi, In the C standard of 1999 additional mathematical functions are added. For example, beside the traditional double sin(double x); there are also the functions: float sinf(float x);...
2
by: Shi Jin | last post by:
Hi there, I have been thinking this for quite a while: if we are considering the number of different representations of one type,say int and float, is there any difference as long as they are...
7
by: Johnathan Doe | last post by:
I can google search to find the range of values that can be represented in a float by reading up on the IEEE std, but is that the same as how many distinct values that can go in a float type? ...
11
by: Marc Pelletier | last post by:
Hello, I am having trouble implementing the following callback: CNCSError CECWCompressor::WriteReadLine(UINT32 nNextLine, void **ppInputArray) where ppInputArray is a 3 by x array. The...
4
by: buntyindia | last post by:
Hi, I have a calculator with seven textBox to add Float numbers upto 2 decimal: I have created following function in js: function total_expenses() { // reteriving all the values from...
9
by: bizt | last post by:
Hi, I am using the following function to validate a forms value as an integer function isNumeric(str){ var numericExpression = /^+$/; if(str.match(numericExpression)){ return true; }else{
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.