How to get the first two digits after the decimal point of a float number in Go

1 Answer

0 votes
package main

import (
	"fmt"
	"strconv"
	"strings"
)

func main() {
	f := 375.487052

	s := strconv.FormatFloat(f, 'f', -1, 64)

	pointPos := strings.Index(s, ".")

	if pointPos != -1 && pointPos+1 < len(s) {
		fmt.Println(s[pointPos+1 : pointPos+3])
	}
}



/*
run:

48

*/

 



answered Sep 18, 2024 by avibootz
...