Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com What are the differences and similarities of parsefloat() and parseint()?
 User Name Remember Me? Password

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 05-02-2012, 05:38 AM PM User | #1 Taro Regular Coder     Join Date: Oct 2011 Location: Geraldton, Ontario Posts: 155 Thanks: 1 Thanked 1 Time in 1 Post What are the differences and similarities of parsefloat() and parseint()? Hello, As a novice, I'm not sure which one to use though. I'm trying to do a calculation with numbers in JavaScript using variables, particularly adding variables. Any guided understanding of this concept is welcome. For example: Code: ```n = parseint(n) document.write(n += m)``` __________________ Element ID Webs Support Helper Your friendly neighborhood Taroman.
 05-02-2012, 08:49 AM PM User | #2 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,489 Thanks: 200 Thanked 2,471 Times in 2,449 Posts As its name implies, parseInt() parses a string value and returns an integer. Syntax:- parseInt(string, radix) If the radix parameter is omitted, JavaScript assumes the following: - If the string begins with "0x", the radix is 16 (hexadecimal) - If the string begins with "0", the radix is 8 (octal). This feature is deprecated - If the string begins with any other value, the radix is 10 (decimal) parseFloat() returns the real (decimal or floating point) value of the number parsed from a string value. If the first character in the string is a number, it parses the string until it reaches the end of the number, and returns the number as a number, not as a string. You should also take a look at the Number() function. Note that as always Javascript is case-sensitive and parseint will return an error. "We have to remember that horses are only human ...." - Racing commentator __________________ All the code given in this post has been tested and is intended to address the question asked. Unless stated otherwise it is not just a demonstration.
05-02-2012, 08:59 AM   PM User | #3
Dormilich
Senior Coder

Join Date: Jan 2010
Location: Behind the Wall
Posts: 3,087
Thanks: 11
Thanked 327 Times in 323 Posts
note: although parseInt() parses the input as Integer, the number returned is a float* (because float (IEEE 754 double) is the only datatype for numbers in JavaScript)

* - might be important for large integers
__________________
Quote:
 The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
André Behrens, NY Times Software Developer

 05-02-2012, 10:40 AM PM User | #4 felgall Master Coder     Join Date: Sep 2005 Location: Sydney, Australia Posts: 6,097 Thanks: 0 Thanked 579 Times in 569 Posts parseFloat() handles very small numbers and very big numbers - eg parseFloat('1e100') returns the same number (1 googol - 1 followed by a hundred 0) as Math.pow(10,100) but without having to calculate what 10 to the 100th power is. parseInt() handles number bases between 2 and 36 to convert numbers to base 10 - for example parseInt('100',2) returns 4 and parseInt('ff',16) returns 255 and parseInt('z',36) returns 35. As a side effect both drop any characters from the end that are not numbers - eg parseInt('39',8) will return 3 and ignore the 9 This side effect is useful when processing style related numbers eg. parseInt('250px',10) returns 250. If you just want to convert a string to a number without the number being very big/small or in a different number base then Number() is the function to use as it just handles regular numbers without all the extra overheads needed to handle numbers such as '1.35e-25' and '0xffaa' - for example Number('120') returns 120. __________________ Stephen Learn Modern JavaScript - http://javascriptexample.net/ Helping others to solve their computer problem at http://www.felgall.com/ Beginners need to advise whether they want to learn "Latin" JavaScript for Netscape 3 or "Italian" JavaScript for modern browsers. Last edited by felgall; 05-02-2012 at 10:57 AM..
05-02-2012, 09:59 PM   PM User | #5
Taro
Regular Coder

Join Date: Oct 2011
Location: Geraldton, Ontario
Posts: 155
Thanks: 1
Thanked 1 Time in 1 Post
I am grateful for the thorough replies.

Quote:
 Originally Posted by Philip M As its name implies, parseInt() parses a string value and returns an integer. Syntax:- parseInt(string, radix) If the radix parameter is omitted, JavaScript assumes the following: - If the string begins with "0x", the radix is 16 (hexadecimal) - If the string begins with "0", the radix is 8 (octal). This feature is deprecated - If the string begins with any other value, the radix is 10 (decimal) parseFloat() returns the real (decimal or floating point) value of the number parsed from a string value. If the first character in the string is a number, it parses the string until it reaches the end of the number, and returns the number as a number, not as a string. You should also take a look at the Number() function. Note that as always Javascript is case-sensitive and parseint will return an error. "We have to remember that horses are only human ...." - Racing commentator
Well, I did get an error in the function I was writing, and forget to make the 'F' or 'I' upper case. Sometimes I try to figure out what was wrong with the code, after looking at the misguided JavaScript errors from the HTML-Kit software I was using.

Quote:
 Originally Posted by Dormilich note: although parseInt() parses the input as Integer, the number returned is a float* (because float (IEEE 754 double) is the only datatype for numbers in JavaScript) * - might be important for large integers
Good to know.

Quote:
 Originally Posted by felgall parseFloat() handles very small numbers and very big numbers - eg parseFloat('1e100') returns the same number (1 googol - 1 followed by a hundred 0) as Math.pow(10,100) but without having to calculate what 10 to the 100th power is. parseInt() handles number bases between 2 and 36 to convert numbers to base 10 - for example parseInt('100',2) returns 4 and parseInt('ff',16) returns 255 and parseInt('z',36) returns 35. As a side effect both drop any characters from the end that are not numbers - eg parseInt('39',8) will return 3 and ignore the 9 This side effect is useful when processing style related numbers eg. parseInt('250px',10) returns 250. If you just want to convert a string to a number without the number being very big/small or in a different number base then Number() is the function to use as it just handles regular numbers without all the extra overheads needed to handle numbers such as '1.35e-25' and '0xffaa' - for example Number('120') returns 120.
I did not realize that there is so much to JavaScript functions, like the parseInt('39',8), rather than just putting a single value. I do find the Number() function very simple to use.
__________________
Element ID

Webs Support Helper

Your friendly neighborhood Taroman.

Last edited by Taro; 05-02-2012 at 10:03 PM..

05-02-2012, 11:18 PM   PM User | #6
felgall
Master Coder

Join Date: Sep 2005
Location: Sydney, Australia
Posts: 6,097
Thanks: 0
Thanked 579 Times in 569 Posts
Quote:
 Originally Posted by Taro like the parseInt('39',8), rather than just putting a single value.
You need to specify two parameters when calling parseInt as otherwhise it tries to guess the number base from the content - as Philip mentioned in his earlier post. By specifying the base in the second parameter you avoid the number being misinterpreted.

A common error is people using parseInt to convert a day or month string into a number instead of simply using Number(). Where the day or month is '08' or '09' then some browsers will assume the leading zero makes the number octal rather than decimal and since in base 8 you count 01, 02, 03, 04, 05, 06, 07, 010, 011 and there are no such numbers as 8 and 9 the person who wrote the code is left wondering why they are getting 0 when they expected 8 or 9. Specifying the second parameter as 10 resolves it - althougfh simply substituting Number() is an even more efficient resolution.

parseInt('39',8) is an example of the side effect that parseInt and parseFloat have where any non-numeric portion on the right of the number is simply discarded. In this example the returned value is 3 and the 9 is discarded since 9 is not a number in octal - just as with parseInt('08') the value returned might be zero if the browser interprets the leading zero as an indicator that the number is octal (not all browsers treat a leading 0 as octal so the result returned could be either 0 or 8 depending on the browser - there are even some browsers that would return 8 if you are using strict JavaScript and 0 if you didn't specify "use strict")..
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/

Beginners need to advise whether they want to learn "Latin" JavaScript for Netscape 3 or "Italian" JavaScript for modern browsers.

Last edited by felgall; 05-02-2012 at 11:21 PM..

 05-03-2012, 09:20 AM PM User | #7 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,489 Thanks: 200 Thanked 2,471 Times in 2,449 Posts In short, if you do use parseInt(), you must always specify the radix (normally 10). Number() is normally the best choice. __________________ All the code given in this post has been tested and is intended to address the question asked. Unless stated otherwise it is not just a demonstration.

 Bookmarks

 Tags float, integer, parse, variable

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 05:44 PM.