Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

How to Convert string to float type in Golang?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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 (
    "fmt"
    "strconv"
)
  
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
    fmt.Println(b1)
  
    a2 := "-2.514"
    b2, _ := strconv.ParseFloat(a2, 32)
    fmt.Println(b2)
  
    fmt.Println(b1 + b2)
}

Output:

-2.514
-2.5139999389648438
-5.027999938964843
My Personal Notes arrow_drop_up
Last Updated : 19 May, 2020
Like Article
Save Article
Similar Reads