CodeBlanks Logo Icon CodeBlanks

int and float

Learn integer and floating-point types.

int and float

In Go, int is used for whole numbers and float64 for decimals. int is 32 or 64 bits depending on the platform; on today's computers it is 64 bits.

yas := 36          // int
boy := 1.72        // float64
fmt.Println(yas, boy)

When a specific size is needed there are also int8, int16, int32, int64 and the unsigned uint types.

Integer and decimal operations

With integers, % gives the remainder: 7 % 2 → 1. With decimals, division gives the real result: 7.0 / 2 → 3.5. Because decimals are stored in binary, 0.1 + 0.2 isn't exactly 0.3; for money it's safer to keep cents as an integer.

Practice Division and remainder

Declare a := 17 and b := 5. Print a / b, a % b and float64(a) / float64(b) with one fmt.Println.

Starter code

main.go
package main

import "fmt"

func main() {
    // buraya yaz
}

Sample solution

main.go
package main

import "fmt"

func main() {
    a := 17
    b := 5
    fmt.Println(a/b, a%b, float64(a)/float64(b))
}

Expected output

3 2 3.4

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does <code>7 / 2</code> give in Go?

  • 3 Correct answer
  • 3.5
  • 4
  • An error

Explanation: Dividing two ints gives an int and the fraction is dropped. For 3.5, write 7.0 / 2.

2 Find the remainder

Question
kalan := 17 ____ 5  // 2
Answer
kalan := 17 % 5  // 2

Explanation: The % operator gives the remainder of integer division.

3 Value and type

Term Meaning
42 int
3.14 float64
7 / 2 3 (int)
7.0 / 2 3.5 (float64)

All lessons in Beginner