float

Full name
float
Library
Built-in
Syntax

class float([n])

Description

The float function returns a real number from a number or a text string.

If the argument is a text string, it should contain the decimal point (though not required, see example below), optionally preceded by a sign. If no argument is added, the function returns the real number 0.0. The argument can also be a text string representing a "nan" (not a number) or a positive or negative infinity. More exactly, the argument must comply with the following grammar after removing whitespaces at the beginning and end of the string:

sign            :: = "+" | "-"
infinite        :: = "Infinity" | "inf"
nan             :: = "nan"
numeric_value   :: = real_number | infinity | nan
text_string     :: = [sign] numeric_value

Parameters
  • n: (optional) number or text string to convert to real number.
Examples

The function can convert whole numbers to their real equivalent format. The possible + sign before the number is not required:

Float function. Example of use

 

The argument can also be a real number:

Float function. Example of use

 

If the argument is a text string, it can include the decimal point or not:

Float function. Example of use

 

If it is a whole number, you can include the decimal separator but not include the decimal part:

Float function. Example of use

 

The text string may or may not include the sign:

Float function. Example of use

 

The text string can include blank spaces at the beginning or end of it, spaces that are not considered in the conversion:

Float function. Example of use

 

The number represented in the text string can be in scientific notation:

Float function. Example of use

 

The argument can represent the "nan" value (not a number):

Float function. Example of use

 

It can also represent infinity, both positive and negative:

Float function. Example of use

 

If no argument is included, the function returns zero:

print(float())

Float function. Example of use

 

Submitted by admin on Sun, 01/20/2019 - 13:25