The unofficial CircuitPython Reference

float()

description

Python provides builtin functions that explicitly convert between data types. This is commonly known as casting. It is often necessary to cast values when they are used as arguments in a function call. It is generally necessary to cast values whenever a function or other process cannot implicitly convert a given variable to the appropriate datatype. For example when printing a string followed by a numeric type the numeric type must be cast to a string for string concatenation in the print() call to work.

float(variable)  is used to cast values to the float type. Casting an int like 12 to a float will a decimal place like 12.0 . Casting a string to float will convert numeric strings like "123.45" to float values like 123.45.

syntax

cast int to float

my_var = 1605

float(my_var)

cast string to float

my_string = "16.05"

float(my_string)

methods

parameters

returns

example code

serial output:

code.py output:

1605

16.05

see also: