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

1 Answer

0 votes
package main

import (
    "fmt"
    "math"
)

func firstDecimalDigit(f float64) int {
    return int(math.Floor(math.Abs(f) * 10)) % 10
}

func main() {
    f := 983.6571
    fmt.Println(firstDecimalDigit(f)) // 6
}


/*
run:

6

*/

 



answered Oct 30, 2024 by avibootz
edited Sep 16 by avibootz

Related questions

...