How to round a floating-point number to an integer in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "math"
)

func main() {
    x := 9382.4;
    y := int(math.Floor(x + 0.5))
    fmt.Println(y)
    
    x = 9382.5;
    y = int(math.Floor(x + 0.5))
    fmt.Println(y)
}


 
/*
run:

9382
9383

*/

 



answered May 14, 2025 by avibootz

Related questions

1 answer 55 views
1 answer 64 views
1 answer 62 views
1 answer 104 views
2 answers 190 views
...