Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 486 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rounding a double value to x number of decimal places in swift

#31
For many applications, you need exact number of decimal places to round.

For some others, you do not have such a constraint and want to "compress" output size, but still want to avoid converting number to strings (and vice versa later) like in exporting JSON having millions of numbers.

In that case you may use 'trick' to round significand (mantissa), not whole number. In such a case you will end up with final decimal places
and most of numbers will still have at most e.g. 3 decimal places (if needed) preserved while some will have slightly more.


This is what you might expect with this approach while still working with decimal numbers:

> 5.2472
>
> 5.2516
>
> 5.2556
>
> 5.26
>
> 5.264

instead of:

>5.24731462499949
>
>5.251488374999099
>
>5.25566283399894
>
>5.259839374999501
>
>5.264012208999702









let value = 5.24731462499949

print(value)
// 5.24731462499949

let valueSignificandRounded = round((1000 * 10) * value.significand) / (1000 * 10)

let valueRounded = CGFloat(sign: v.sign, exponent: v.exponent, significand: valueSignificandRounded)

print(valueRounded)
// 5.2472

Reply

#32
For ease to use, I created an extension:
```
extension Double {
var threeDigits: Double {
return (self * 1000).rounded(.toNearestOrEven) / 1000
}

var twoDigits: Double {
return (self * 100).rounded(.toNearestOrEven) / 100
}

var oneDigit: Double {
return (self * 10).rounded(.toNearestOrEven) / 10
}
}

var myDouble = 0.12345
print(myDouble.threeDigits)
print(myDouble.twoDigits)
print(myDouble.oneDigit)
```
The print results are:
```
0.123
0.12
0.1
```

Thanks for the inspiration of other answers!
Reply

#33
if you want after the comma there is only 0 for the round, try this:

extension Double {
func isInteger() -> Any {
let check = floor(self) == self
if check {
return Int(self)
} else {
return self
}
}
}

let toInt: Double = 10.0
let stillDouble: Double = 9.12

print(toInt.isInteger) // 10
print(stillDouble.isInteger) // 9.12
Reply

#34
Swift 4 Best Way

This is what worked for me to round to 2 decimal places


let val = round(100 * scale) / 100
Reply

#35
In iOS 15 / macOS 12 new inline number formatters have been introduced.

Without any rounding rule it rounds like *taught in school*. The syntax

let value = 1.543240
let rounded = value.formatted(.number.precision(.fractionLength(2))))

rounds down to 1.54 and

let value = 1.545240
let rounded = value.formatted(.number.precision(.fractionLength(2))))

rounds up to 1.55, `fractionLength` specifies the number of fractional digits

To force rounding down add a rounding rule


let rounded = value.formatted(.number.rounded(rule: .down).precision(.fractionLength(2))))



Reply

#36
Building on Yogi's answer, here's a Swift function that does the job:

func roundToPlaces(value:Double, places:Int) -> Double {
let divisor = pow(10.0, Double(places))
return round(value * divisor) / divisor
}

---
As extension with rounding rule:
```swift
extension Double {
/// - returns: Rounded value with specific round rule and precision
func roundToPlaces(_ rule: FloatingPointRoundingRule? = . toNearestOrEven, precision: Int) -> Double {
let divisor = pow(10.0, Double(precision))
return (self * divisor).rounded(rule) / divisor
}
}

print(0.123456.roundToPlaces(.down, precision: 4)) // 0.1234
print(0.123456.roundToPlaces(.up, precision: 4)) // 0.1235
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through