The Number object's parseFloat() method

Last updated: February 24, 2025

The Number.parseFloat() method parses a string argument and returns a floating-point number.

Syntax

Number.parseFloat(string)

Parameters

string:

  • The value to parse. If not a string, it's converted to one.

  • Leading whitespace is ignored

  • If the first character cannot be converted to a number, Number.parseFloat() returns NaN

Return value

The Number.parseFloat() method returns a floating-point number parsed from the given string. If the string cannot be converted to a number, it returns NaN (Not a Number).

Examples

Example 1: Basic floating-point parsing

In the code below, the function parseFloatEx1() demonstrates basic string-to-float conversion.

function parseFloatEx1() {
  // Basic parsing
  Logger.log(Number.parseFloat("3.14"));       // 3.14
  Logger.log(Number.parseFloat("42"));         // 42.0 (integer as float)
  Logger.log(Number.parseFloat("42.0"));       // 42.0 (integer as float)
  
  // Handles scientific notation
  Logger.log(Number.parseFloat("1.234e5"));    // 123400.0
  Logger.log(Number.parseFloat("1.234E-5"));   // 1.234E-5
  
  // Ignores non-numeric characters after the number
  Logger.log(Number.parseFloat("3.14px"));     // 3.14
  
  // Leading whitespace is ignored
  Logger.log(Number.parseFloat("   99.99   ")); // 99.99
  
  // If first character isn't numeric, returns NaN
  Logger.log(Number.parseFloat("Apps123.45"));  // NaN
  
  // Negative numbers
  Logger.log(Number.parseFloat("-42.5"));      // -42.5
}

Output:

3.14

42.0

42.0

123400.0

1.234E-5

3.14

99.99

NaN

-42.5

Example 2: Handling different number formats

In the code below, the function parseFloatEx2() shows how to parse different number formats.

function parseFloatEx2() {
  // Decimal formats
  Logger.log(Number.parseFloat("3.14159"));    // 3.14159
  Logger.log(Number.parseFloat(".5"));         // 0.5 (leading zero optional)
  
  // Currency values (ignores non-numeric characters)
  Logger.log(Number.parseFloat("$100.50"));    // NaN ($ is not valid at start)
  Logger.log(Number.parseFloat("100.50$"));    // 100.5
  
  // Thousands separators
  Logger.log(Number.parseFloat("1,000.50"));   // 1.0 (comma breaks parsing)
  
  // Removing commas first
  Logger.log(Number.parseFloat("1,000.50".replace(/,/g, ""))); // 1000.5
  
  // Percentage values
  Logger.log(Number.parseFloat("75%"));        // 75.0 (% is ignored)
  
  // Converting percentage to decimal
  let percentage = "75%";
  let decimal = Number.parseFloat(percentage) / 100;
  Logger.log(decimal);                        // 0.75
}

Output:

3.14159

0.5

NaN

100.5

1.0

1000.5

75.0

0.75

Example 3: Handling edge cases

In the code below, the function parseFloatEx3() demonstrates handling of edge cases.

function parseFloatEx3() {
  // Special cases
  Logger.log(Number.parseFloat("Infinity"));    // Infinity
  Logger.log(Number.parseFloat("-Infinity"));   // -Infinity
  Logger.log(Number.parseFloat("NaN"));         // NaN
  
  // Empty or invalid inputs
  Logger.log(Number.parseFloat(""));            // NaN
  Logger.log(Number.parseFloat(null));          // NaN
  Logger.log(Number.parseFloat(undefined));     // NaN
  
  // Very large or small numbers
  Logger.log(Number.parseFloat("1.7976931348623157e+308"));  // Max value in AppsScript
  Logger.log(Number.parseFloat("4.9E-324"));                  // Min value in AppsScript
  
  // Parsing stops at first invalid character
  Logger.log(Number.parseFloat("3.14.15"));     // 3.14 (stops at second decimal)
  Logger.log(Number.parseFloat("99USD"));       // 99.0
}

Output:

Infinity

-Infinity

NaN

NaN

NaN

NaN

1.7976931348623157E308

4.9E-324

3.14

99.0

Example 4: Comparing with global parseFloat()

In the code below, the function parseFloatEx4() demonstrates how Number.parseFloat() relates to the global parseFloat() function. They're identical but using Number.parseFloat() makes it clear you're using a Number method

function parseFloatEx4() {
  // Number.parseFloat() is identical to the global parseFloat() function
  Logger.log(Number.parseFloat === parseFloat);  // true
  
  // Both produce the same results
  Logger.log(Number.parseFloat("3.14"));        // 3.14
  Logger.log(parseFloat("3.14"));               // 3.14
  
  // Both have the same behavior with edge cases
  Logger.log(Number.parseFloat("$99.99"));      // NaN
  Logger.log(parseFloat("$99.99"));             // NaN  
}

Output:

true

3.14

3.14

NaN

NaN

Example 5: Practical use in Apps Script

In the code below, the function parseFloatEx5() demonstrates practical uses of Number.parseFloat() in Google Apps Script.

function parseFloatEx5() {
  // Working with spreadsheet values
  let cellValue = "42.5%";
  let numericValue = Number.parseFloat(cellValue) / 100;
  Logger.log("Percentage as decimal: " + numericValue);  // 0.425
  
  // Handling currency values from a spreadsheet
  let priceValue = "$1,234.56";
  // Remove currency symbol and commas
  let cleanPrice = priceValue.replace(/[$,]/g, "");
  let price = Number.parseFloat(cleanPrice);
  Logger.log("Price value: " + price);  // 1234.56
  
  // Processing coordinates
  let latLongText = "37.7749° N, 122.4194° W";
  let parts = latLongText.split(',');
  let latitude = Number.parseFloat(parts[0]);
  let longitude = Number.parseFloat(parts[1]);
  Logger.log("Coordinates: " + latitude + ", " + longitude);  // 37.7749, 122.4194
  
  // Calculating with floating-point numbers
  let measurements = ["1.5m", "2.25m", "3.75m"];
  let sum = 0;
  for (let i = 0; i < measurements.length; i++) {
    sum += Number.parseFloat(measurements[i]);
  }
  Logger.log("Total length: " + sum + "m");  // 7.5m
}

Output:

Percentage as decimal: 0.425

Price value: 1234.56

Coordinates: 37.7749, 122.4194

Total length: 7.5m

Number.parseFloat() vs Number.parseInt()

Unlike Number.parseInt(), Number.parseFloat() doesn't take a radix parameter since floating-point numbers are always base-10. Here's how they compare:

function compareParseFloatAndParseInt() {
  let value = "3.14";
  
  Logger.log(Number.parseFloat(value));  // 3.14 (preserves decimal portion)
  Logger.log(Number.parseInt(value, 10)); // 3.0 (truncates decimal portion)
  
  // Scientific notation
  let scientific = "1.23e4";
  Logger.log(Number.parseFloat(scientific));  // 12300.0 (recognizes scientific notation)
  Logger.log(Number.parseInt(scientific, 10)); // 1.0 (stops at first non-digit)
}

Output:

3.14

3.0

12300.0

1.0

Number.parseFloat() vs global parseFloat()

In modern JavaScript, Number.parseFloat() and the global parseFloat() function are identical.The main reasons to use Number.parseFloat() instead of the global parseFloat() are:

  • Consistency with other Number methods

  • Makes it clear you're performing a number conversion

  • May be preferred in code style guides

Both functions have identical functionality, parameters, and return values.

Common Use Cases

The Number.parseFloat() method is useful in many scenarios when working with Google Apps Script:

  • Financial calculations: Processing currency values and performing precise decimal calculations.

  • Data validation: Converting and validating numeric input from various sources.

  • Scientific computations: Handling measurement data with decimal precision.

  • Coordinate processing: Working with geographic coordinates or other positioning data.

  • Percentage conversions: Converting between percentage representations and decimal values.

  • Unit conversions: Converting between different units of measurement.

Conclusion

In this tutorial, you learned how to use the Number.parseFloat() method to convert strings to floating-point numbers in Google Apps Script. It's especially useful when working with numeric data that includes decimal places, such as financial information, scientific measurements, and percentages.

Sign up to be notified when I publish new content

By signing up you agree to the Privacy Policy & Terms.