type conversion
Turning a value of one type into another, such as the string "23" into the integer 23.
In more detail
Many languages convert by writing the type name like a function call, such as int("23") in Python or float64(used) in Go. Some languages convert between number types automatically; others, such as Go, require every conversion to be written out. Converting a floating-point number to an integer usually throws the fraction away, and a conversion that makes no sense, such as turning "hello" into a number, fails with an error.
Examples
float64(used) / float64(total)int("23") + 5str(23) + "5"Related terms
- int — The common integer type in many languages, holding whole numbers within a fixed range set by its size in bits.
- floating-point number — A number with a fractional part stored in binary, which can only approximate most decimal values.
- integer division — Dividing one whole number by another and keeping only the whole part of the result, so 7 / 2 is 3.
- static typing — A language rule that every variable has one fixed type, known when the program is compiled, so type mistakes are caught before it runs.
- data type — The kind of a value, such as a whole number, text or a date, which decides what you can do with it and how it compares and sorts.
- constructor call — A call that creates a new object, written as the type or class name used like a function, such as list(), set("abc") or Point(1, 2).
- ValueError — The Python exception raised when a function gets an argument of the right type but an unacceptable value, such as int("hello").
- input() — The built-in function that shows a prompt, waits for the user to type a line and press Enter, and returns what they typed as a string.
Taught in
- Variables, Typing, and Zero Values — Concurrent Systems Programming with Go
- Parsing JSON with encoding/json — Concurrent Systems Programming with Go
- Strings, Delimiters And Concatenation — Hands-on Python: Complete Video Course & Book
- Entry Objects — Hands-on Python: Complete Video Course & Book