{"id":24795,"date":"2021-11-02T09:47:50","date_gmt":"2021-11-02T04:17:50","guid":{"rendered":"https:\/\/python-programs.com\/?p=24795"},"modified":"2021-11-05T20:36:08","modified_gmt":"2021-11-05T15:06:08","slug":"python-float-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-float-method-with-examples\/","title":{"rendered":"Python float() Method with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python complex() Method with Examples<\/a> The float() function returns a floating-point number from the specified value.<\/p>\n Syntax:<\/strong><\/p>\n Parameter Values:<\/strong><\/p>\n value: A number or string that can be converted to a floating-point number.<\/p>\n Return Value:<\/strong><\/p>\n The float() method yields:<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Similarly, check it out for other values and print the result<\/p>\n Output:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Fed up with searching various pages for the list of Python Built in Functions<\/a>? Look at the tutorial linked here and explore all coding samples of built-in functions of python.<\/p>\n In the previous article, we have discussed Python complex() Method with Examples float() Method in Python: The float() function returns a floating-point number from the specified value. Syntax: float(value) Parameter Values: value: A number or string that can be converted to a floating-point number. Return Value: The float() method yields: If an argument is passed, …<\/p>\n
\nfloat() Method in Python:<\/strong><\/p>\nfloat(value)<\/pre>\n
\n
Given Number = 14<\/pre>\n
The given number's{ 14 } Floating-point number = 14.0<\/pre>\n
Given Number = \"-25.431\" (# given in string format)<\/pre>\n
The given number's{ -25.431 } Floating-point number = -25.431<\/pre>\n
float() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\ngvn_numbr = 14\r\n# Pass the given number as an argument to the float() function to get the\r\n# the floating-point number of a given number.\r\n# Store it in another variable.\r\nrslt = float(gvn_numbr)\r\n# Print the floating-point number of a given number.\r\nprint(\"The given number's{\", gvn_numbr,\r\n \"} Floating-point number = \", rslt)\r\n<\/pre>\n
The given number's{ 14 } Floating-point number = 14.0\r\n<\/pre>\n
gvn_numbr = \"-25.456\"\r\nrslt = float(gvn_numbr)\r\nprint(\"The given number's{\", gvn_numbr,\r\n \"} Floating-point number = \", rslt)\r\n<\/pre>\n
The given number's{ -25.456 } Floating-point number = -25.456<\/pre>\n
float() for infinity and Nan(Not a number) values<\/h5>\n
print(float(\"InFiNiTy\"))\r\nprint(float(\"infinity\"))\r\nprint(float(\"nan\"))\r\nprint(float(\"NaN\"))\r\nprint(float(\"inf\"))\r\nprint(float(\"InF\"))\r\n<\/pre>\n
inf\r\ninf\r\nnan\r\nnan\r\ninf\r\ninf<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the number as a string as user input using the input() function\r\n# and store it in a variable.\r\ngvn_numbr = input(\"Enter some random number = \")\r\n# Pass the given number as an argument to the float() function to get the\r\n# the floating-point number of a given number.\r\n# Store it in another variable.\r\nrslt = float(gvn_numbr)\r\n# Print the floating-point number of a given number.\r\nprint(\"The given number's{\", gvn_numbr,\r\n \"} Floating-point number = \", rslt)\r\n<\/pre>\n
Enter some random number = -19\r\nThe given number's{ -19 } Floating-point number = -19.0<\/pre>\n
\n