pykot/converters

Convert various types to other types, e.g. int -> string.

Procs

func toFloat(c: char): float {....raises: [ValueError], tags: [], forbids: [].}
Convert a digit, represented as a char, to float.

Example:

doAssert '0'.toFloat == 0.0
doAssert '5'.toFloat == 5.0
func toFloat(f: float): float {....raises: [], tags: [], forbids: [].}

Convert a float to float.

It seems useless, but if you don't know the exact type of a variable in a chain and it happens to be a float, it does no harm.

Example:

doAssert (3.14).toFloat == 3.14
func toFloat(s: string): float {....raises: [ValueError], tags: [], forbids: [].}
Convert a string to float.

Example:

doAssert "3.2".toFloat == 3.2
func toInt(digit: char): int {....raises: [ValueError], tags: [], forbids: [].}
Convert a digit, represented as a char, to int.

Example:

doAssert '0'.toInt == 0
doAssert '5'.toInt == 5
func toInt(n: int): int {....raises: [], tags: [], forbids: [].}

Convert an int to int.

It seems useless, but if you don't know the exact type of a variable in a chain and it happens to be an int, it does no harm.

Example:

doAssert 2.toInt == 2
doAssert 123.toInt == 123
func toInt(s: string): int {....raises: [ValueError], tags: [], forbids: [].}
Convert a string to int.

Example:

doAssert "42".toInt == 42
doAssert "-576".toInt == -576
func toIntPart(f: float): int {....raises: [], tags: [], forbids: [].}

Convert a float to int and keep just the integer part.

Keep just the integer part, do no rounding. Python's int(...) works like this. However, system.toInt(f: float): int does rounding.

Example:

doAssert (3.2).toIntPart == 3
doAssert (3.8).toIntPart == 3
doAssert (-3.2).toIntPart == -3
doAssert (-3.8).toIntPart == -3

Templates

template str(a: untyped): string
Convert something to string.

Example:

doAssert str(42) == "42"
doAssert str(3.14) == "3.14"
template toStr(a: untyped): string
Convert something to string.

Example:

doAssert 42.toStr == "42"
doAssert (3.14).toStr() == "3.14"