Easiest way to crop float to 2 decimal places?

Is there a way in Swift to crop a float to 2 decimal places so you can do further calculations with it? All the streams that I saw relate to casting in a string, and I cannot figure out how to use it mathematically.

I tried using the extension (found on this forum), believing that I could throw away the float after truncation, but I end up where I started with another, not truncated float. I need the return value to be in the fourth step (e.g. 6.50, 6.75, 5.25, etc.), and what I get are the results, such as 6.990022 ....

There should be an easy way to do this, but I hit the wall. Thanks in advance...

Here's the problem:

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) 
+7
decimal swift truncation
source share
2 answers

You cannot exactly round Float or Double to two decimal digits. The reason is that these data types use a binary floating-point representation, and cannot accurately represent digits, such as 0.1 or 0.01. See for example

But you said:

I need my return value to be in the fourth step (e.g. 6.50, 6.75, 5.25, etc.)

and this is possible because 0.25 = 2 -2 can be represented exactly as a floating point number.

The round() function rounds a floating-point number to the nearest integer value. To round to the nearest quarter, you just need to “scale” the calculation with a factor of 4:

 func roundToNearestQuarter(num : Float) -> Float { return round(num * 4.0)/4.0 } roundToNearestQuarter(6.71) // 6.75 roundToNearestQuarter(6.6) // 6.5 
+10
source share

If you need to work with true precision (for example, for currency-related applications), you probably want to use NSDecimalNumber instead of a floating point.

The above approach can be applied to NSDecimalNumbers, as shown below. In this example, the “step” that you round can be whatever you select, just set the “increment” accordingly.

 let number: NSDecimalNumber = 100.52 let increment: NSDecimalNumber = 0.25 let handler = NSDecimalNumberHandler(roundingMode: NSRoundingMode.RoundBankers, scale: 0, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false) // Rounds to the nearest whole number let result = number.decimalNumberByDividingBy(increment).decimalNumberByRoundingAccordingToBehavior(handler).decimalNumberByMultiplyingBy(increment) 

For more information on rounding with NSDecimalNumber see here: How to combine NSDecimalNumber in swift?

And yes, working with NSDecimalNumber is an awfully detailed way to do the math, but it's not complicated. If you often carry out a project with their participation, I recommend that you consider creating extensions to the Swift operator so that you can manage them more efficiently. Check out a good example: https://gist.github.com/mattt/1ed12090d7c89f36fd28

0
source share

All Articles