79548629

Date: 2025-04-01 12:30:34
Score: 1
Natty:
Report link

When you cast a float64 to an int it will just remove the fractional part. By adding 0.5 to the number you will get the behaviour of it rounding the number.

package main

import "fmt"

func main() {
    fmt.Printf("roundInt(1.0): %v\n", roundInt(1.0))
    fmt.Printf("roundInt(1.4): %v\n", roundInt(1.4))
    fmt.Printf("roundInt(1.5): %v\n", roundInt(1.5))
    fmt.Printf("roundInt(11.9): %v\n", roundInt(11.9))
}

func roundInt(f float64) int {
    return int(f + .5)
}
Reasons:
  • Has code block (-0.5):
  • Starts with a question (0.5): When you
  • Low reputation (1):
Posted by: mr070