Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parseInt problem

Status
Not open for further replies.

tramp1982

Programmer
Joined
Dec 23, 2003
Messages
2
Location
GB
Hi,

Can you tell me the equivalent rule to parseInt but with more than whole numbers.
I need to add 2 numbers together as variable but they are stored as strings and concatenate.
So I used the parseInt function & ignored any decimals I used.

Quick reply would be great.

Richard
ritchie@central-point.co.uk
 
You can also use convert 2 string 'floats' to a regular float with:

Code:
var newFloat = ('1.2' - 0) + ('3.4' - 0);
alert(newFloat); // gives 4.6

Dan
 
An effective hack I use sometimes:

Code:
var number1 = '1.2345';
var number2 = '6.789';
var result = number1 - 0 + number2;

As long as the variable is indeed a number, subtracting 0 from it will 'cast' it to a number... and once javascript considers it a number, it will treat the + as a plus (not a concatenate).

Jeff
 
while the hacks might involve less typing, IMHO it is poor practice and obscures the intention of the code. using the proper methods makes clear what you are doing.




=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Couldn't agree more with you... from the free online dictionary database
"a quick job that produces what is needed, but not well."

Your initial reply to use parseFloat() was the correct answer. I was offering another solution that is no less effective across all browsers.

For the record, I don't think I've ever used my own technique on a client site (nor on any of my own production sites). I use it a lot when I want a quick answer inline.

Given the fact this is a forum of Javascript Professionals, maybe my post was not all that appropriate.

Jeff
 
HACK also "An appropriate application of ingenuity".
source = an earlier free e-book I downloaded called "hacker jargon" or something. Had some good input - usefule in these time of script kiddies, hacker .v cracker etc.

In earlier languages like FORTRAN, you HAD to have inline conversion like : XFLOAT = IINT + 0.00.

(2)
There's a small problem with parseInt. If the number start with a high order zero, the second character isn't "x", and the radix isn't explicitly mentioned or mentioned as zero, the parsent() assumes the number to be an octal and you know what then happens to 8 and 9. parseInt() will quietly give wrong results.

(By the way 0x assumes it's a hexa decimeal and may under very exceptional condition (like a,b,c,d e and f only) blithely convert invalid values to numbers.

This condition sometimes arises in data entry in web forms where users are keying in codes from existing paper documents.

The solution is to write a function that trims highorder zeros and then supply it to parseInt() or indeed parseFloat().

I mean, that the "hack" may indeed be very effective.

End
 
Great stuff every1, just what I needed.

Tramp1982
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top