Welcome to Bucaro TecHelp!

Bucaro TecHelp
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Holiday Gift Guide
Holiday Gift Guide

Use JavaScript parseInt() and parseFloat() to Convert Strings to Numbers

When you use different data types in operations, JavaScript tries it's best to perform the proper data type conversions to make the operation work, but sometimes it can't figure out what you have in mind. Take for example the simple addition operation shown below.

4 + "4"

You might assume the result would be the number 8, but instead the result would be the text string "44". Another example is shown below.

4.4 + "4.4"

You might assume the result would be the number 8.8, but instead the result would be the text string "4.44.4".

If the results of a mathematical operation in your script are not what you expected, make sure that you convert any numbers represented as strings to the actual number data type before performing the operation.

JavaScript provides two built-in functions to convert numbers represented as strings to actual numbers, parseInt() and parseFloat().

parseInt()

parseInt() converts an integer represented as string to the number data type. An integer is a whole number with no decimal point. The statement shown below shows the previous operation using parseInt().

4 + parseInt("4")

This time the result would be the number 8 as expected.

parseFloat()

parseFloat() converts a floating-point number represented as string to the number data type. The statement shown below shows the previous operation using parseFloat().

4.4 + parseFloat("4.4")

This time the result would be the number 8.8 as expected.

Some Other Useful JavaScript Built-in Functions for Determining Data Type

typeof() Function

The typeof() function returns a string indicating the data type of a literal or variable passed to it. In the example below the message box would display string.

var result = 4.4 + "4.4";
alert(typeof(result));

In the example below the message box would display number.

var result = 4.4 + parseFloat("4.4");
alert(typeof(result));

isNaN() Function

The isNaN() function returns a boolean indicating if a literal or variable passed to it is an illegal number. It returns true if the value is NaN (Not-a-Number), and false if it is a number. In the example below the message box would display true (result is not a number).

var result = 4.4 + "4.4";
alert(isNaN(result));

If the results of a mathematical operation in your script are not what you expected, these JavaScript built-in functions; parseInt(), parseFloat(), typeof(), and isNaN() should help solve the problem.

More Java Script Code:
• Calculators for Your Website : Fahrenheit to Celsius Converter
• Code for a Less annoying Popup Window
• Calculators For Your Web Site
• Prototype Keyword Gives JavaScript Object Oriented Inheritance
• Easy Code for Animated Banner - Squeeze Text
• Easy Moving Popup Code
• Web Site Menus : Which Section Am I In?
• Make Your Own Graphical Digital Clock
• Use JavaScript parseInt() and parseFloat() to Convert Strings to Numbers
• JavaScript to Replace All Occurrences of a Substring Within a String

RSS Feed RSS Feed

Follow Stephen Bucaro Follow @Stephen Bucaro



Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2017 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268