ADVERTISEMENT
ADVERTISEMENT

Golang strconv.AppendFloat() Function with Examples

Golang | strconv.AppendFloat() Function: Here, we are going to learn about the AppendFloat() function of the strconv package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 06, 2021

strconv.AppendFloat()

The AppendFloat() function is an inbuilt function of the strconv package which is used to append the string form of the given floating-point number f (as generated by FormatFloat() function), to dst and returns the extended buffer. Where dst is the first parameter of []byte type and f is the second parameter of float64 type.

It accepts five parameters (dst, f, byte, prec, bitSize) and returns the extended buffer.

Syntax:

func AppendFloat(
    dst []byte, 
    f float64, 
    fmt byte, 
    prec, bitSize int) []byte

Parameter(s):

  • dst : A byte array (or byte slices) in which we have to append the floating-point number as a string.
  • f : A floating-point number to be appended in dst.
  • fmt : Formatting format.
  • prec : Precision.
  • bitSize : Bit size (32 for float32, 64 for float64).

Return Value:

The return type of AppendFloat() function is a []byte, it returns the extended buffer after appending the given floating-point value.

Example 1:

// Golang program to demonstrate the
// example of strconv.AppendFloat() Function

package main

import (
	"fmt"
	"strconv"
)

func main() {
	x32 := []byte("float32:")
	fmt.Println("Before AppendFloat()")
	fmt.Println(string(x32))

	// Appending floating-point value
	x32 = strconv.AppendFloat(x32, 123.456789, 'E', -1, 32)
	fmt.Println("After AppendFloat()")
	fmt.Println(string(x32))

	fmt.Println("------------")

	x64 := []byte("float64:")
	fmt.Println("Before AppendFloat()")
	fmt.Println(string(x64))

	// Appending floating-point value
	x64 = strconv.AppendFloat(x64, 123.456789, 'E', -1, 64)
	fmt.Println("After AppendFloat()")
	fmt.Println(string(x64))
}

Output:

Before AppendFloat()
float32:
After AppendFloat()
float32:1.2345679E+02
------------
Before AppendFloat()
float64:
After AppendFloat()
float64:1.23456789E+02

Example 2:

// Golang program to demonstrate the
// example of strconv.AppendFloat() Function

package main

import (
	"fmt"
	"strconv"
)

func main() {
	x32 := []byte("float32:")
	fmt.Println("Before appending...")
	fmt.Println("x32:", string(x32))
	fmt.Println("Length(x32): ", len(x32))

	// Appending a floating-point value
	x32 = strconv.AppendFloat(x32, 123.456789, 'E', -1, 32)
	x32 = strconv.AppendFloat(x32, 123.456789, 'E', -1, 32)

	fmt.Println("After appending...")
	fmt.Println("x32:", string(x32))
	fmt.Println("Length(x32): ", len(x32))

	fmt.Println("----------")

	x64 := []byte("float64:")
	fmt.Println("Before appending...")
	fmt.Println("x64:", string(x64))
	fmt.Println("Length(x64): ", len(x64))

	// Appending a floating-point value
	x64 = strconv.AppendFloat(x64, 123.456789, 'E', -1, 64)
	x64 = strconv.AppendFloat(x64, 123.456789, 'E', -1, 64)

	fmt.Println("After appending...")
	fmt.Println("x64:", string(x64))
	fmt.Println("Length(x64): ", len(x64))
}

Output:

Before appending...
x32: float32:
Length(x32):  8
After appending...
x32: float32:1.2345679E+021.2345679E+02
Length(x32):  34
----------
Before appending...
x64: float64:
Length(x64):  8
After appending...
x64: float64:1.23456789E+021.23456789E+02
Length(x64):  36

Golang strconv Package »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.