Skip to main content
added 461 characters in body
Source Link
Krtko
  • 1.1k
  • 18
  • 24

Building off of holex's answer. I did

func accurateRound(value: Double) -> Int {
            
            var d : Double = value - Double(Int(value))
            
            if d < 0.5 {
                return Int(value)
            } else {
                return Int(value) + 1
            }
        }

-edit extension edition-

I also recently turned this into an extension for Floats thought I'd share as well :)

extension Float {
    func roundToInt() -> Int{
        var value = Int(self)
        var f = self - Float(value)
        if f < 0.5{
            return value
        } else {
            return value + 1
        }
    }
}

This is makes it so you can just be like

var f : Float = 3.3
f.roundToInt()

Building off of holex's answer. I did

func accurateRound(value: Double) -> Int {
            
            var d : Double = value - Double(Int(value))
            
            if d < 0.5 {
                return Int(value)
            } else {
                return Int(value) + 1
            }
        }

Building off of holex's answer. I did

func accurateRound(value: Double) -> Int {
            
            var d : Double = value - Double(Int(value))
            
            if d < 0.5 {
                return Int(value)
            } else {
                return Int(value) + 1
            }
        }

-edit extension edition-

I also recently turned this into an extension for Floats thought I'd share as well :)

extension Float {
    func roundToInt() -> Int{
        var value = Int(self)
        var f = self - Float(value)
        if f < 0.5{
            return value
        } else {
            return value + 1
        }
    }
}

This is makes it so you can just be like

var f : Float = 3.3
f.roundToInt()
Source Link
Krtko
  • 1.1k
  • 18
  • 24

Building off of holex's answer. I did

func accurateRound(value: Double) -> Int {
            
            var d : Double = value - Double(Int(value))
            
            if d < 0.5 {
                return Int(value)
            } else {
                return Int(value) + 1
            }
        }