parseFloat() in JavaScript

  • The parseFloat() function is a built-in function in JavaScript that parses a string and returns a floating-point number. It takes a string as its argument and attempts to convert it to a floating-point number.
  • Here is an example:

    let str = "3.14";
    let num = parseFloat(str);
    console.log(num); // output: 3.14

  • The parseFloat() function can be useful in situations where you need to convert a string to a floating-point number, such as when you are performing mathematical calculations.
  • One of the main benefits of using parseFloat() is that it can handle a variety of input formats. For example, it can handle strings with or without leading and trailing whitespace, as well as strings with decimal points in different positions. It can also handle strings with both positive and negative numbers.
  • Here are some examples of input strings that parseFloat() can handle:

    parseFloat("3.14");        // 3.14
    parseFloat("  3.14  ");    // 3.14
    parseFloat("3");           // 3
    parseFloat("3.0");         // 3
    parseFloat("-3.14");       // -3.14
    parseFloat("1.2e3");       // 1200
    parseFloat("1.2e-3");      // 0.0012
    parseFloat("Hello world"); // NaN

  • One thing to keep in mind is that parseFloat() will stop parsing the string as soon as it encounters an invalid character. For example, if you pass in a string that starts with a non-numeric character, parseFloat() will return NaN.
  • It only parses the beginning of the string: parseFloat() will only parse the beginning of the input string. Any characters that come after the first invalid character will be ignored. For example, parseFloat("3.14 is the value") will return 3.14.
  • It ignores leading zeros: parseFloat() ignores leading zeros in the input string. For example, parseFloat("003.14") will return 3.14.
  • It can handle scientific notation: parseFloat() can handle input strings that are in scientific notation, such as 1.23e-4. For example, parseFloat("1.23e-4") will return 0.000123.
  • It does not handle non-numeric input: parseFloat() is designed to parse numeric input only. If you pass in a non-numeric input, such as a boolean value or an object, it will return NaN.
  • It is a global function: parseFloat() is a global function in JavaScript, which means that it can be called from anywhere in your code. You do not need to import it from a module or library.
  • It is not a constructor: parseFloat() is not a constructor function with the new keyword to create a new instance of it. For example, new parseFloat("3.14") will result in a TypeError.
  • It can be used with the Number() constructor: If you need to parse a string to a number in JavaScript, you can use the Number() constructor. However, note that Number() behaves differently from parseFloat() in certain cases. For example, Number(" 3.14 ") will return 3.14, while Number("3.14 is the value") will return NaN.
  • The parseFloat() function does not take a radix parameter, as it always assumes a base 10 input. However, you can use the parseInt() function with a radix parameter to parse numbers in other bases, such as binary, octal, or hexadecimal.
  • parseFloat() can also parse the string "NaN" to the special floating-point value NaN, which stands for "Not a Number". This value is used to represent undefined or unrepresentable mathematical operations, such as the result of dividing zero by zero.
  • In summary, parseFloat() is a useful function in JavaScript for converting strings to floating-point numbers. Its ability to handle a variety of input formats makes it a versatile tool for performing mathematical calculations in JavaScript.

No comments:

Post a Comment