In Swift, is there a way to truncate a float to 2 decimals, such that you can perform further calculations with it? All of the threads I've seen deal with casting to a string, which I can't figure out how to then use mathematically.
I tried using an extension (found on this forum), figuring I could cast back to float after the truncation, but I end up where I started, with another, non-truncated float. I need my return value to be in quarter steps (i.e. 6.50, 6.75, 5.25, etc), and what I'm ending up with, are results like 6.990022....
There has to be a simple way to do this, but I'm hitting a wall. Thanks in advance...
Here's the issue:
func roundToNearestQuarter(#power : Float) -> String {
var errorToLowerQuarterRaw : Float = power % 0.25 // 0.210000038146973
var errorToLowerQuarterString = errorToLowerQuarterStepRaw.string(2) // "0.21"
var errorToLowerQuarter = NSString(string: errorToLowerQuaterStepString).floatValue // 0.209999993443489
// more code
}
roundToNearestQuater(6.71)