OneBite.Dev - Coding blog in a bite size

convert variable from float to string in Go

Code snippet on how to convert variable from float to string in Go

  myFloat := 5.999
  myString := strconv.FormatFloat(myFloat, 'f', -1, 64)

This code converts a variable, named myFloat, which is of type float (in this example, it is equal to 5.999) to a variable of type string. To do this, we use the built-in strconv package and its FormatFloat function. The first parameter is the float value that will be converted, the second parameter is the formatting which is ‘f’ in this case, and the third and fourth parameters denote the precision and data type (in this case, 64 bit). The conversion is done and the result is passed to the myString variable.

go