Golang : How to read float value from standard input ?
This is a continuation from previous tutorial on how to read integer value from standard input. For this tutorial, we will learn how to read float value from standard input. Below is the code to do that :
package main
import (
"fmt"
"strconv"
)
func main() {
var f float64
fmt.Println("Enter a float value : ")
_, err := fmt.Scanf("%f", &f)
if err != nil {
fmt.Println(err)
}
fmt.Printf("You have entered : %f \n", f)
fmt.Println("Alternative output ", strconv.FormatFloat(f, 'f', 6, 64))
}
See also : Golang : How to read integer value from standard input ?
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+11.9k Golang : Display list of countries and ISO codes
+17.8k Golang : Check if a directory exist or not
+15.3k Golang : Convert date format and separator yyyy-mm-dd to dd-mm-yyyy
+13.8k Golang : Fix image: unknown format error
+20.8k Golang : Create and resolve(read) symbolic links
+5k Golang : Calculate half life decay example
+8.3k Linux/Unix : fatal: the Postfix mail system is already running
+11.1k Golang : Find age or leap age from date of birth example
+8.7k Golang : How to capture return values from goroutines?
+10.6k Golang : Sieve of Eratosthenes algorithm
+8.5k Yum Error: no such table: packages
+37.4k Golang : Comparing date or timestamp