Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions




Numeric Conversion Functions

We can convert a number from one type to another. A conversion may involve a loss of precision because we've reduced the number of bits available. A conversion may also add a false sense of precision by adding bits which don't have any real meaning.

We'll call these factory functions because they are a factory for creating new objects from other objects. The idea of factory function is a very general one, and these are just the first of many examples of this pattern.

Also, this section is only an introduction. A more complete list of conversion functions is provided in the section called “Conversion Functions”.

Conversion Function Definitions

There are a number of conversions from one numeric type to another.

int ( x ) → integer

Generates an integer from the object x . If x is a floating point number, digits to the right of the decimal point are truncated as part of creating an integer. If the floating point number is more than about 10 digits, a long integer object is created to retain the precision. If x is a long integer that is too large to be represented as an integer, there's no conversion. If x is a string, the string is parsed to create an integer value. Complex values can't be turned into integers directly.

float ( x ) → float

Generates a float from object x . If x is an integer or long integer, a floating point number is created. Note that long integers can have a large number of digits, but floating point numbers only have approximately 16 digits; there can be some loss of precision. Complex values can't turned into floating point numbers directly.

long ( x ) → long

Generates a long integer from x . If x is a floating point number, digits to the right of the decimal point are truncated as part of creating a long integer.

complex ( real , [ imag ]) → complex

Generates a complex number from real and imag .

If this “ complex ( real , [imag] )” syntax synopsis is confusing, you might want to see Syntax For Newbies.

Syntax For Newbies

We'll show optional parameters to functions by surrounding them with []'s. In the case of complex, there will be two different ways to use the function, with one parameter, real , or with two parameters, real , imag . We don't actually enter the []'s, they're just hints as to what the two forms of the function are.

In the case of “ complex ( real , [ imag ] )”, we have two choices.

complex( 2.0, 3.0 )

complex( 4.0 )


Conversion Function Examples

float ( x ) creates a floating point number equal to the string or number x . If a string is given, it must be a valid floating point number: digits, decimal point, and an exponent expression. Examples: float("6.02E24"), float(23). This is handy when doing division to prevent getting the simple integer quotient.

For example:

print float(22)/7, 22/7

3.14285714286 3

int ( x ) creates an integer equal to the string or number x . If a string is given, it must be a valid decimal integer string. Examples: int("1243"), int(3.14159).

long ( x ) creates a long integer equal to the string or number x . If a string is given, it must be a valid decimal integer. The expression long(2) has the same value as the literal 2L. Examples: long(6.02E23), long(2).

For example:

print long(2)**64


This shows the range of values possible with 64-bit integers, available on larger computers.

Complex is not as simple as the others. A complex number has two parts, real and imaginary. Conversion to complex typically involves two parameters.

complex ( r , [ i ] ) creates a complex number with the real part of r ; if the second parameter, i , is given, this is the imaginary part of the complex number, otherwise the imaginary part is zero. The string must be a valid complex number. Examples:

print complex(3,2), complex(4), complex("3+4j")

(3+2j) (4+0j) (3+4j)

Note that the second parameter, with the imaginary part of the number, is optional. This leads to a number of different ways to call this function. In the example above, we used three variations: two numeric parameters, one numeric parameter and one string parameter.

  Published under the terms of the Open Publication License Design by Interspire