Skip to content
Related Articles

Related Articles

strconv.AppendFloat() Function in Golang With Examples

View Discussion
Improve Article
Save Article
  • Last Updated : 21 Apr, 2020

Go language provides inbuilt support to implement conversions to and from string representations of basic data types by strconv Package. This package provides an AppendFloat() function which is used to append the string form of the floating-point number. To access AppendFloat() function you need to import strconv Package in your program.

Syntax:

func AppendFloat(num []byte, val float64, fmt byte, prec, bitSize int) []byte

This function will append the string form of the floating-point number val, as generated by FormatFloat, to num and returns the extended buffer.

Example 1:




// Golang program to illustrate
// strconv.AppendFloat() Function
package main
  
import (
    "fmt"
    "strconv"
)
  
func main() {
  
    // Using AppendFloat() function
    val1 := []byte("Float32 value: ")
    val1 = strconv.AppendFloat(val1, 4.5683568954, 'E', -1, 32)
    fmt.Println(string(val1))
  
    val2 := []byte("Float64 value: ")
    val2 = strconv.AppendFloat(val2, 6.7415678653, 'E', -1, 64)
    fmt.Println(string(val2))
  
}


Output:

Float32 value: 4.568357E+00
Float64 value: 6.7415678653E+00

Example 2:




// Golang program to illustrate
// strconv.AppendFloat() Function
package main
  
import (
    "fmt"
    "strconv"
)
  
func main() {
  
    // Using AppendFloat() function
    val1 := []byte("Float32 value: ")
    val1 = strconv.AppendFloat(val1, 
          5.5636895645, 'E', -1, 32)
      
    fmt.Println(string(val1))
      
    // using len and cap function
    fmt.Println("Length: ", len(val1))
    fmt.Println("Capacity: ", cap(val1))
  
    val2 := []byte("Float64 value: ")
    val2 = strconv.AppendFloat(val2,
        1.741532678653, 'E', -1, 64)
      
    fmt.Println(string(val2))
      
    // using len and cap function
    fmt.Println("Length: ", len(val2))
    fmt.Println("Capacity: ", cap(val2))
  
}


Output:

Float32 value: 5.5636897E+00
Length:  28
Capacity:  32
Float64 value: 1.741532678653E+00
Length:  33
Capacity:  64

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!