Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 25th, 2005, 11:09 AM   #1
Navid
Hobbyist Programmer
 
Navid's Avatar
 
Join Date: Feb 2005
Location: Canada
Posts: 187
Rep Power: 4 Navid is on a distinguished road
Send a message via MSN to Navid
If it has a decimal

x = 4.2

If (x "has a decimal")

how do i get JS to check if it has a decimal?
Navid is offline   Reply With Quote
Old Mar 26th, 2005, 9:26 PM   #2
Navid
Hobbyist Programmer
 
Navid's Avatar
 
Join Date: Feb 2005
Location: Canada
Posts: 187
Rep Power: 4 Navid is on a distinguished road
Send a message via MSN to Navid
nevermind, i figured a solution
Navid is offline   Reply With Quote
Old Mar 27th, 2005, 5:33 PM   #3
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
if(parseInt(x) != x)
tempest is offline   Reply With Quote
Old Apr 1st, 2005, 8:52 AM   #4
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4 mackenga is on a distinguished road
Another method would be to check:

// return nonzero if x has a fractional part; nonzero otherwise
function hasDecimalPoint(x) {
  return (x.toString().indexOf(".") + 1);
}

(Haven't tested that but I think it should work; if not, the general idea is that if there's no decimal point, indexOf(".") in the string version (x.toString()) should be -1.)

Actually, assuming it does work, it will have a slightly weird behaviour if passed something that isn't a number at all... but used as it expects to be, it should be a decent solution.

Last edited by mackenga; Apr 1st, 2005 at 8:56 AM. Reason: Edited code example to make it a function
mackenga is offline   Reply With Quote
Old Apr 1st, 2005, 9:30 AM   #5
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
function isDecimal(field)
{

var validTxt = /^[0-9.]+$/
var msg = "Not a valid decimal.";
if (! validTxt.test(field.value))
{
alert(msg);
return false;
}
else
{
return true;
}
}
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Apr 2nd, 2005, 3:33 PM   #6
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4 mackenga is on a distinguished road
Oooh, I must confess I've never seen a regexp used in JavaScript. Is that a feature of the language or does it use some sort of library?
mackenga is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:28 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC