跪拜 Guibai
← All articles
Backend · Go

Go Type Conversion: When Truncation, Not Rounding, Strips Your Decimals

By 小满zs ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Go's truncation-on-conversion behavior is a common source of off-by-one and precision bugs when porting code from languages that round. Knowing that `ParseBool` accepts `1`/`0` and single-character `t`/`f` prevents brittle input validation that rejects valid configuration values.

Summary

Numeric type conversion in Go uses the type name as a function: `int(3.14)` yields `3`, discarding the decimal entirely. Every integer and float width gets its own conversion function, from `int8()` through `float64()`. The language refuses to mix strings and numbers in arithmetic, so `"10" + 20` is a compile error.

String-number conversion lives in the `strconv` package. `Itoa` (Int to ASCII) turns an integer into a string; `Atoi` (ASCII to Int) does the reverse and returns both a result and an error. Boolean conversion via `ParseBool` recognizes `1`, `t`, `T`, `TRUE`, `true`, and `True` as true, plus `0`, `f`, `F`, `FALSE`, `false`, and `False` as false — anything else fails. `FormatBool` produces only the lowercase strings `"true"` or `"false"`.

Five exercises walk through numeric truncation, `Itoa`/`Atoi` round-trips, `ParseBool` error handling, and a combined task that parses a price string and a discount flag, applies an 80% multiplier, and formats the result back to a string.

Takeaways
`int(3.1415)` truncates to `3` — the decimal is discarded, not rounded.
Every numeric type name doubles as a conversion function: `int8()`, `uint32()`, `float64()`, and so on.
`strconv.Itoa` converts an int to its decimal string representation.
`strconv.Atoi` returns two values: the parsed int and an error; ignoring the error with `_` is common in quick examples but dangerous in production.
`strconv.ParseBool` treats `1`, `t`, `T`, `TRUE`, `true`, `True` as true, and `0`, `f`, `F`, `FALSE`, `false`, `False` as false.
`strconv.FormatBool` always outputs `"true"` or `"false"` — no uppercase variants.
Go forbids direct arithmetic between strings and numbers; `"10" + 20` is a compile-time error.
Conclusions

Go's choice to make type-name-as-function the conversion syntax keeps the language surface small but hides the truncation behavior behind a call that looks like a constructor — newcomers often expect rounding.

The `ParseBool` truth table is broader than many configuration parsers, which means a value like `1` from an environment variable will parse correctly where other languages might reject it.

Returning an error from `Atoi` and `ParseBool` forces explicit error handling, but the exercises show the common beginner pattern of discarding it with `_` — a habit that leads to silent zero-value bugs when input is malformed.

Concepts & terms
Truncation (in Go numeric conversion)
When converting a floating-point number to an integer type, Go discards the fractional part entirely rather than rounding. `int(9.99)` yields `9`, not `10`.
strconv.Itoa / Atoi
`Itoa` (Integer to ASCII) converts an int to its decimal string. `Atoi` (ASCII to Integer) parses a decimal string into an int and returns both the result and an error.
strconv.ParseBool
Parses a string into a bool, accepting a wider set of values than just `"true"`/`"false"`: `1`, `t`, `T`, `TRUE`, `true`, `True` for true; `0`, `f`, `F`, `FALSE`, `false`, `False` for false.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗