Golang How to convert string to float, int

eye-catch Golang

Data conversion is often used but I often forget how to do it. I created this post as a cheat sheet so that many developers can understand the result.

The following constant values are used in the examples.

const (
    Bit0  = 0
    Bit8  = 8
    Bit32 = 32
    Bit64 = 64
    DEC   = 10
    HEX   = 16
    BI    = 2
)
Sponsored links

int to something

int to string

I guess a decimal base (10) is used in most cases to convert an integer to string. So I recommend using strconv.Itoa because it’s simple.

intValue := 128
fmt.Println(strconv.FormatInt(int64(intValue), DEC)) // 128
fmt.Println(strconv.FormatInt(int64(intValue), HEX)) // 80
fmt.Println(strconv.FormatInt(int64(14), HEX))       // e
fmt.Println(strconv.FormatInt(int64(intValue), BI))  // 10000000
fmt.Println(strconv.Itoa(intValue))                  // 128
fmt.Println(fmt.Sprint(intValue))                    // 128

result := fmt.Sprintf("%d", intValue)
fmt.Println(result) // 128
result = fmt.Sprint(intValue)
fmt.Println(result) // 128

It’s possible to use fmt.Sprint friend functions but I don’t think it’s the main usage of this function. It could be used if the data type is unknown but strconv.Itoa should be used if the data type is known.

Note that the result uses lower-case letters. If upper-case is needed, convert it yourself.

int to float32/float64

It’s simple. Cast the value with the type.

intValue := 128
fmt.Println(float64(intValue)) // 128
fmt.Println(float32(intValue)) // 128
// fmt.Printf format %f has arg intValue of wrong type int
fmt.Printf("%f\n", intValue)          // %!f(int=128)
fmt.Printf("%f\n", float64(intValue)) // 128.000000
fmt.Printf("%f\n", float32(intValue)) // 128.000000
fmt.Printf("%.2f\n", float64(intValue)) // 128.00

Don’t forget to cast the value if Sprintf or Printf needs to be used. Add the precision value like %.2f if trailing 0 is necessary for the floating value.

int to int64/uint64

Don’t convert a negative value to unsigned int. The intention is not clear.

intValue := 128
fmt.Println(int64(intValue))  // 128
fmt.Println(uint64(intValue)) // 128
intNegative := -1
fmt.Println(uint64(intNegative))     // 18446744073709551615
fmt.Println(uint64(intNegative + 1)) // 0
Sponsored links

string to something

string to int32/int64

strconv.ParseInt returns a value and error. Choose the right base number DEC(10), HEX(16), or BI(2) depending on the value in the string.

strValue := "128"
fmt.Println(strconv.ParseInt(strValue, DEC, Bit64))  // 128 <nil>
fmt.Println(strconv.ParseInt(strValue, HEX, Bit64))  // 296 <nil>
fmt.Println(strconv.ParseInt(strValue, BI, Bit64))   // 0 strconv.ParseInt: parsing "128": invalid syntax
fmt.Println(strconv.ParseInt("11110000", BI, Bit64)) // 240 <nil>
fmt.Println(strconv.ParseInt(strValue, DEC, Bit0))   // 128 <nil>
fmt.Println(strconv.ParseInt(strValue, DEC, Bit8))   // 127 strconv.ParseInt: parsing "128": value out of range
fmt.Println(strconv.ParseInt("127", DEC, Bit8))      // 127 <nil>

The third parameter is the size of the value. Since the range of int8 is from -128 to 127, value out of range error occurs. Choose the desired bit size if you need to assign the converted value to the fixed bit-sized variable. Assign 0 or 64 if there’s no such limitation.

string to float64

Assign Bit64 if there’s no limitation. It’s accurate.

strFloatValue := "128.123"
fmt.Println(strconv.ParseFloat(strFloatValue, Bit32)) // 128.1230010986328 <nil>
fmt.Println(strconv.ParseFloat(strFloatValue, Bit64)) // 128.123 <nil>

string to int

Since strconv.ParseInt() returns int64, it needs to be converted to int. If there’s no bit size limitation, strconv.Atoi() is simpler.

strValue := "128"
result2, _ := strconv.ParseInt(strValue, DEC, Bit64)
fmt.Println(int(result2))           // 128
fmt.Println(strconv.Atoi(strValue)) // 128 <nil>

float to something

strconv.FormatFloat() has many options. It’s difficult to read the differences at a first glance. Examples are necessary to have.

float64 to string

If there’s no specification, use the first one strconv.FormatFloat(floatValue, 'f', -1, Bit64). If precision is needed, specify the values depending on the specification.

floatValue := 128.123456
fmt.Println(strconv.FormatFloat(floatValue, 'f', -1, Bit64)) // 128.123456
fmt.Println(strconv.FormatFloat(floatValue, 'f', 0, Bit64))  // 128
fmt.Println(strconv.FormatFloat(floatValue, 'f', 2, Bit64))  // 128.12
fmt.Println(strconv.FormatFloat(floatValue, 'f', 5, Bit64))  // 128.12346
fmt.Println(strconv.FormatFloat(floatValue, 'f', 10, Bit64)) // 128.1234560000

fmt.Println(strconv.FormatFloat(floatValue, 'b', -1, Bit64))        // 4507943349211095p-45
fmt.Println(strconv.FormatFloat(floatValue, 'e', -1, Bit64))        // 1.28123456e+02
fmt.Println(strconv.FormatFloat(floatValue, 'E', -1, Bit64))        // 1.28123456E+02
fmt.Println(strconv.FormatFloat(floatValue, 'E', 3, Bit64))         // 1.281E+02
fmt.Println(strconv.FormatFloat(floatValue, 'g', -1, Bit64))        // 128.123456
fmt.Println(strconv.FormatFloat(floatValue, 'G', -1, Bit64))        // 128.123456
fmt.Println(strconv.FormatFloat(12345678901234567, 'G', -1, Bit64)) // 11.2345678901234568E+16
fmt.Println(strconv.FormatFloat(12345678901234567, 'G', 5, Bit64))  // 11.2346E+16
fmt.Println(strconv.FormatFloat(floatValue, 'x', -1, Bit64))        // 0x1.003f359ff4fd7p+07
fmt.Println(strconv.FormatFloat(floatValue, 'X', -1, Bit64))        // 0X1.003F359FF4FD7P+07

'g' and 'G' shows a different result depending on the value. If it’s big, it shows with an exponential format.

float64 to int/int8/int16/int32/int64

The floating value is truncated by default. If it needs to be rounded, use math.Round() or math.Ceil().

floatValue := 128.123456
fmt.Println(int(floatValue))   // 128
fmt.Println(int8(floatValue))  // -128
fmt.Println(int16(floatValue)) // 128
fmt.Println(int32(floatValue)) // 128
fmt.Println(int64(floatValue)) // 128
floatValue2 := 128.99999
fmt.Println(int(floatValue2))         // 128
fmt.Println(int(math.Round(128.599))) // 129
fmt.Println(int(math.Round(128.499))) // 128
fmt.Println(int(math.Ceil(128.001)))  // 129
fmt.Println(int(math.Trunc(128.999))) // 128

Struct to string

Comments

Copied title and URL