By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Go separates numbers into two families. Whole numbers live in int: 0, 7, -3, 1250. Numbers with a decimal point live in float64: 0.5, 12.5, 0.0837. The two families do not mix by accident. int and float64 are different types, and Go makes you say when you want to cross from one to the other.
When you divide one int by another, the result is also an int. Any fraction is dropped, not rounded. 1250 / 100 is 12, not 12.5. That sounds like a trap until you see the pair of operators that work together: / gives the whole part and % gives the remainder.
package main
import "fmt"
func main() {
cents := 1250
dollars := cents / 100
leftover := cents % 100
fmt.Println("dollars:", dollars)
fmt.Println("cents left:", leftover)
fmt.Println("total:", dollars, "dollars and", leftover, "cents")
}This pair of operators lets Penny show a price such as 1250 cents as $12.50 without ever using a float: 12 dollars and 50 cents left over. The diagram below follows the whole path this lesson teaches, from whole-number arithmetic to text a person can read.
When you do need the fraction, convert one side with float64(...) before you divide. A conversion writes the target type in front of the value, like a small instruction: treat this value as a float for this calculation. float64(cents) / 100 works because 100 also fits in a float, so the division happens in floating point.
Converting the other way with int(...) truncates. Go will not convert a float to an int automatically, because losing the fraction is a decision you should make on purpose. The math package adds tools for rounding properly, such as math.Round.
package main
import (
"fmt"
"math"
)
func main() {
cents := 1250
whole := cents / 100
exact := float64(cents) / 100
fmt.Println("whole:", whole)
fmt.Println("exact:", exact)
fmt.Println("int(exact):", int(exact))
fmt.Println("rounded:", math.Round(exact))
}A number typed into a terminal, or saved in a file, arrives as text. If a program reads "1250" as a string and tries to divide it, the compiler stops the program before it runs. Text and numbers are different types, and Go does not guess.
Use the strconv package to parse text into numbers. strconv.Atoi parses a whole number, and strconv.ParseFloat parses one with a decimal point. Atoi is the short name for strconv.ParseInt in base 10, and both return two values: the number and an error. When the text is valid, the error is nil, which means nothing went wrong.
package main
import (
"fmt"
"strconv"
)
func main() {
cents, err := strconv.Atoi("1250")
if err != nil {
fmt.Println("not a number:", err)
return
}
rate, err := strconv.ParseFloat("0.0837", 64)
if err != nil {
fmt.Println("not a number:", err)
return
}
fmt.Println("cents:", cents)
fmt.Println("rate:", rate)
}Penny stores every amount as an int number of cents: 1250 means $12.50. There are two reasons. First, integer division and the remainder operator give exact answers, so a total of many expenses stays exact. Second, floats store numbers in binary and cannot represent every decimal fraction exactly. Tiny errors creep in, and over hundreds of expenses they add up.
package main
import "fmt"
func main() {
a := 0.1
b := 0.2
fmt.Println(a + b)
fmt.Println(a+b == 0.3)
}Formatting for humans is a separate job, done once at the end. You already have the tool: divide by 100 and keep the remainder. Step 8 adds fmt.Printf, which makes $12.50 simple to build.
Penny recorded an expense of 1875 cents. In a program, compute the whole dollars, the cents left over, and the exact float value. Print all three with clear labels. Change the amount to 50 cents and check that your output still reads correctly: 0 dollars and 50 cents. Then parse the text "4200" with strconv.Atoi and print the value and the error to see what success looks like.
Key takeaways
int and float64 are different types; convert explicitly with int(...) and float64(...).
Integer division truncates, and the % operator returns the remainder.
strconv.Atoi and strconv.ParseFloat turn text into numbers and report an error when the text is wrong.
Penny stores money as int cents so totals stay exact; format for humans only when printing.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.