How to Convert string to float type in Golang?

ParseFloat function is an in-build function in the strconv library which converts the string type into a floating-point number with the precision specified by bit size.

Example: In this example the same string -2.514 is being converted into float data type and then their sum is being printed. Once it is converted to 8 bit-size and other times it is 32 bit-size. Both yield different results because ParseFloat accepts decimal and hexadecimal floating-point number syntax. If a1 or a2 is well-formed and near a valid floating-point number, ParseFloat returns the nearest floating-point number rounded using IEEE754 unbiased rounding which is parsing a hexadecimal floating-point value only rounds when there are more bits in the hexadecimal representation than will fit in the mantissa.





// Golang program to Convert
// string to float type
package main
import (
func main() {
    // defining a string a1
    a1 := "-2.514"
    // converting the string a1 
    // into float and storing it
    // in b1 using ParseFloat
    b1, _ := strconv.ParseFloat(a1, 8)
    // printing the float b1
    a2 := "-2.514"
    b2, _ := strconv.ParseFloat(a2, 32)
    fmt.Println(b1 + b2)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.