Go · Stage 1 – Storing Information · Lesson 14 of 64

Converting Between Types

Convert a value explicitly when you need a different compatible type.

Convert an integer into a float64 by writing the destination type like a function:

Go code

count := 3
decimalCount := float64(count)
fmt.Println(decimalCount)
Line-by-line explanation
  1. Count is inferred as int.
  2. float64(count) creates a decimal-number value.
  3. DecimalCount stores the converted value.
  4. Println displays it.
Expected output
3

Println may display a whole-looking 3 even though the stored type is float64.

Converting a float64 to int removes the decimal part; it does not round:

Go code

price := 4.9
whole := int(price)
fmt.Println(whole)
Line-by-line explanation
  1. Price is inferred as float64.
  2. int(price) creates an integer.
  3. The fractional part is discarded.
  4. Println displays 4.
Expected output
4

The result is truncated toward zero, not rounded to 5.

Converting numbers to written decimal text requires formatting tools introduced later. string(65) does not produce "65"; it represents the Unicode character A.