Detecting float numbers in Javascript
Rules Latest Posts Who's Online
Search
Search Forums
Advanced Search
« Previous123Next »
RSS Feed

Detecting float numbers in Javascript

#  Oct 13, 2009 at 7:20 PM
Join Date:
Nov 25, 2007
Post Count:
1510
 
[VIM]Elhehir 80661
Loading...
Specialty: Pyro
Steam ID: [VIM]Elhehir
Clan: {TF2F} [VIM]
SquigMember
 
Hello! I'm doing a Javascript homework at the moment and I need some help: I would like to know if some number is a floating point number.

Actually, I have a solution, albeit not very elegant: regular expressions for detecting dots/commas...
I've thought that there may be a better way to do it as I don't really want to resort to reg exp since they are especially ugly.

(will use them as a backup solution though)

Got any idea?

Thank you
#  Oct 13, 2009 at 11:23 PM
Join Date:
Nov 30, 2007
Post Count:
7372
 
GManNickG 80805
Loading...
Specialty: Spy
Steam ID: gman_nick
Clan: {TF2F} Community
King SpySquigMember
 
I don't do Javascript, but I'm assuming it only has generic variables right? The question is, how do I determine if X is an integer or not?

Or do you have a string and want to know if it is a character representation of an integer or not?
#  Oct 14, 2009 at 12:20 AM
Join Date:
Nov 25, 2007
Post Count:
1510
 
[VIM]Elhehir 80661
Loading...
Specialty: Pyro
Steam ID: [VIM]Elhehir
Clan: {TF2F} [VIM]
SquigMember
 
I want to know if a number is a float because I want to know if the input is an integer.
I have a string and I want to know if it is a float or a integer. Problem is, javascript doesn't have a different type for those two: both are numbers.

typeof(1) = "number"
typeof(1.24) = "number"
#  Oct 14, 2009 at 12:35 AM
Join Date:
Nov 30, 2007
Post Count:
7372
 
GManNickG 80805
Loading...
Specialty: Spy
Steam ID: gman_nick
Clan: {TF2F} Community
King SpySquigMember
 
Yea, thought so. I guess the only thing to do is check if the string has a period (or comma) in it somewhere:

var str = "123.3";
var i = str.indexOf('.');

// i is 3

var str = "123";
var i = str.indexOf('.');

// i is -1

I'm guessing you could also do something like:

if (parseFloat(var) == parseInt(var)) // was integer

#  Oct 14, 2009 at 1:27 AM
Join Date:
Nov 25, 2007
Post Count:
1510
 
[VIM]Elhehir 80661
Loading...
Specialty: Pyro
Steam ID: [VIM]Elhehir
Clan: {TF2F} [VIM]
SquigMember
 
Finally, did it with regexp:

var regexp_floatcheck = /\./;

if (regexp_floatcheck.test(document.getElementById("case1").value) == true)
    {

           alert("You need to enter an integer in here!")               
    }

else
    {
        blablabla
    }

ugly, but couldn't find anything simpler.
#  Oct 14, 2009 at 1:28 AM
Join Date:
Nov 30, 2007
Post Count:
7372
 
GManNickG 80805
Loading...
Specialty: Spy
Steam ID: gman_nick
Clan: {TF2F} Community
King SpySquigMember
 
indexOf not work?
#  Oct 14, 2009 at 1:29 AM
Join Date:
Nov 30, 2007
Post Count:
7372
 
GManNickG 80805
Loading...
Specialty: Spy
Steam ID: gman_nick
Clan: {TF2F} Community
King SpySquigMember
 
if (document.getElementById("case1").value.indexOf('.') != -1)
    {

           alert("You need to enter an integer in here!")               
    }

else
    {
        blablabla
    }
#  Oct 14, 2009 at 1:41 AM
Join Date:
Nov 02, 2007
Post Count:
1964
 
TuRbINE 79748
Loading...
Specialty: Soldier
Steam ID: thatguy13744
Clan: {TF2F}
Admin ServersSquigMember
 
ITT:  Shit I'm glad I don't have to ever understand.
#  Oct 14, 2009 at 1:43 AM
Join Date:
Nov 30, 2007
Post Count:
7372
 
GManNickG 80805
Loading...
Specialty: Spy
Steam ID: gman_nick
Clan: {TF2F} Community
King SpySquigMember
 
You should see Brainfuck. I want to know that language so I can get all the ladies (like I don't already). This is how you print "Hello World!":
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.

#  Oct 17, 2009 at 10:55 AM
Join Date:
Jan 08, 2008
Post Count:
115
 
Kiki 81968
Loading...
Specialty: Heavy
Clan: RNA
 
Quote:
Originally Posted by GManNickG
You should see Brainfuck. I want to know that language so I can get all the ladies (like I don't already). This is how you print "Hello World!":
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.

Nerddd <3
« Previous123Next »

Copyright 2007-2008 All Rights Reserved

Team Fortress 2


2 Guys 1 Kill: {TF2F} FlyingPenguin - (102 kill assists)
Captures Blocked: Nimbli Bimbli - (26 captures blocked)
Dominator: {TF2F} Bitch Nasty - (25 dominations)
Flags Captured: Corporal Clegg - (11 flags captured)
Flags Defended: MechaEdd - (12 flags defended)
Physician Assisted Homicide: {TF2F}SME®BUI©K - (271 medic kill assists)
Points Captured: {TF2F}SME®BUI©K - (30 points captured)
Revenger: Count_Flakula - (10 revenges)
Spy Power: Chro - (19 objects killed as spy)
Ubercharger: {TF2F}SME®BUI©K - (89 ubercharges)
Axe Man: {TF2F} Ninja Dude - (18 kills with fireaxe)
Axtinguished: {TF2F} BOOSH Rizzilio - (81 kills with axtinguisher)
Batter Up!: soup! - (16 kills with bat)
Biggest Prick: Sam - ClutchKill.com - (8 kills with syringegun)
Blood Sucker: {TF2F}SME®BUI©K - (30 kills with blutsauger)
Broke Backburner: {TF2F} FlyingPenguin - (223 kills with backburner)
Can You Dig It?: {TF2F}.TüRb!NE. - (3 kills with shovel)
Doctor's Certificate: Maero - (16 kills with bonesaw)
Drunken Brawler: [???] Deviruchi - (4 kills with bottle)
Epic Defender: {TF2F} Spy - (9 kills with wrench)
Fire Spitter: {TF2F}blistering - (100 kills with flamethrower)
Flare Gunner: {TF2F} FlyingPenguin - (12 kills with flare gun)
Grenade Spammer: GeneralSheep - (47 kills with pipe)
Grey Matter: STar - (168 snipings as sniper)
Hadouken Master: Xandier - (1 kills with pyro taunt)
Hot Potato: {TF2F} BOOSH Rizzilio - (4 kills with rocket deflection)
I <3 Sasha: {TF2F} Bitch Nasty - (181 kills with minigun)
Knife Maniac: {TF2F} Dartagnan - (109 knifings)
Last Resort: [Ba-El] BoC - (26 kills with smg)
Manslayer: [FRGT] Klare - (10 kills with machete)
Master Fister: Maero - (3 kills with fists)
Peashooter Marksman: Corporal Clegg - (48 kills with scout pistol)
Pickle Squad Award: [NH]Pitt - (18 pickles man handled)
Rocket Scientist: [TF2F]Lancer - (217 kills with rocket)
Scatter Shot: Corporal Clegg - (147 kills with scattergun)
Sentrygun Master: Numenor - (142 kills with sentrygun)
Spam Blocker: {TF2F} BOOSH Rizzilio - (1 kills with pipe deflection)
Spy Checker: {TF2F} Fanlaen - (5 kills with pistol)
Sticky Explosion: {TF2F}Album - (105 kills with pipe remote)
Ubersawer: Psycholord - (27 kills with ubersaw)
Stats Refreshed 6/24/2008 5:00:03 AM CST
AddThis Social Bookmark Button
Fortcast Podcast
Donate
Welcome, you are not logged in.
Login or Register
Team Fortress 2 Fort Team Fortress 2 Fort