ScanSkill

long

long() function returns an expression(or value) converted into a long integer number.

Syntax

long(value, base)

Here,

  • value: Optional. Can be of string, int, float, or long type.
  • base: Optional. Represent the number format. Default: 10

Note: If the value is float, the conversion truncates towards zero. And if the number is not a number

i.e if the base is given, then the number must be a string or Unicode object representing a literal base. The default base is 10. Values allowed are 0 and 2-36.

Examples

  • Convert octal, hex, and binary integer into a proper long integer
>>> long(0o10)
8
>>> long(0x10)
16
>>> long(0b10)
2
  • Convert using a base argument
>>> long('0101', 2)
5L
>>> long('0101', 8)
65L
>>> long('0101', 16)
257L
>>> long('0101', 10)
101L
  • Convert string or float into an integer
>>> long("3.15")
3L
>>> long(3.14)
3L
>>> long(3.14e10)
31400000000L
>>> long(-3.14)
-3L
  • Convert numbers with unary operator into integer
>>> long('-100')
-100L
>>> long('+100')
100
>>> long('100')
100L