correct NSNumberFormatter for many cases in swift - swift

i am having some problems with NSNumberFormatter and Significant Digits in swift.
var test1 : Float = 0.2345
var test2 : Float = 1234.4567
var test3 : Float = 2.234234
i want to show this values in 3 different labels (label1, label2, label3). i am looking for NSNumberFormatter so my variables could look like:
0.24
1234.46
2.23
so all of them have 2 digits after "."
and if first digit is 0 it is visible
right now my code is
var formatter = NSNumberFormatter()
formatter.maximumFractionDigits = 2
formatter.usesSignificantDigits = true
formatter.minimumSignificantDigits = 1
formatter.maximumSignificantDigits = 4
label1.text = formatter.stringFromNumber(var1)
label2.text = formatter.stringFromNumber(var2)
label3.text = formatter.stringFromNumber(var3)

Just leave
formatter.maximumFractionDigits = 2
formatter.minimumSignificantDigits = 1
and add:
formatter.numberStyle = .DecimalStyle
remove the rest:
formatter.usesSignificantDigits = true
formatter.maximumSignificantDigits = 4
It should gives you just number with 2 decimal places.

Related

NSNumberFormatter and .floor roundingMode

Can someone tell me why this happening?
let formatter = NumberFormatter.init()
formatter.numberStyle = .decimal
formatter.usesGroupingSeparator = false
formatter.roundingMode = .floor
formatter.minimumFractionDigits = 2
formatter.maximumFractionDigits = 2
let v = 36
let scale = 10
let float = formatter.string(from: NSNumber(value: Float(v) / Float(scale)))!
let double = formatter.string(from: NSNumber(value: Double(v) / Double(scale)))!
print(float) // 3.59
print(double) // 3.60
When I use Float the result is 3.59 (wrong result in my opinion) and when I use Double the result is 3.60.
I know it is something related to .floor roundingMode, but i don't fully understand the reason.
If you would like to preserve your fraction digits precision it is better to use Swift native Decimal type. That's what it is. You can use the Decimal init(sign: FloatingPointSign, exponent: Int, significand: Decimal) initializer and use your scale exponent and your value significand. Just make sure to negate its value:
extension SignedInteger {
var negated: Self { self * -1 }
}
let v = 36
let scale = 10
let sign: FloatingPointSign = v >= 0 ? .plus : .minus
let exponent = Decimal(scale).exponent.negated
let significand = Decimal(v).significand
let decimal = Decimal.init(sign: sign, exponent: exponent, significand: significand)
let formatted = formatter.string(for: decimal) // "3.60"

Swift. How to set custom number format? With space thouthand separator and two digits after point

I need separate thouthands with space, and two digit after point.
let d1: Double = 20000000.0
let d2: Double = 1.2345
I want to view:
let s1 = String(format: "????", d1) //20 000 000.00
let s2 = String(format: "????", d2) //1.23
How to do it?
let d1: Double = 20000000.0
let d2: Double = 1.2345
let formatter = NumberFormatter()
formatter.groupingSeparator = " "
formatter.numberStyle = .decimal
formatter.minimumFractionDigits = 2
formatter.maximumFractionDigits = 2
formatter.decimalSeparator = "." // Default separator is dependent to the current local.
print(formatter.string(for: d1)) // 20 000 000.00
print(formatter.string(for: d2)) // 1.23

NSNumberFormatter PercentStyle decimal places

I'm using Swift
let myDouble = 8.5 as Double
let percentFormatter = NSNumberFormatter()
percentFormatter.numberStyle = NSNumberFormatterStyle.PercentStyle
percentFormatter.multiplier = 1.00
let myString = percentFormatter.stringFromNumber(myDouble)!
println(myString)
Outputs 8% and not 8.5%, how would I get it to output 8.5%? (But only up to 2 decimal places)
To set the number of fraction digits use:
percentFormatter.minimumFractionDigits = 1
percentFormatter.maximumFractionDigits = 1
Set minimum and maximum to your needs. Should be self-explanatory.
With Swift 5, NumberFormatter has an instance property called minimumFractionDigits. minimumFractionDigits has the following declaration:
var minimumFractionDigits: Int { get set }
The minimum number of digits after the decimal separator allowed as input and output by the receiver.
NumberFormatter also has an instance property called maximumFractionDigits. maximumFractionDigits has the following declaration:
var maximumFractionDigits: Int { get set }
The maximum number of digits after the decimal separator allowed as input and output by the receiver.
The following Playground code shows how to use minimumFractionDigits and maximumFractionDigits in order to set the number of digits after the decimal separator when using NumberFormatter:
import Foundation
let percentFormatter = NumberFormatter()
percentFormatter.numberStyle = NumberFormatter.Style.percent
percentFormatter.multiplier = 1
percentFormatter.minimumFractionDigits = 1
percentFormatter.maximumFractionDigits = 2
let myDouble1: Double = 8
let myString1 = percentFormatter.string(for: myDouble1)
print(String(describing: myString1)) // Optional("8.0%")
let myDouble2 = 8.5
let myString2 = percentFormatter.string(for: myDouble2)
print(String(describing: myString2)) // Optional("8.5%")
let myDouble3 = 8.5786
let myString3 = percentFormatter.string(for: myDouble3)
print(String(describing: myString3)) // Optional("8.58%")
When in doubt, look in apple documentation for minimum fraction digits and maximum fraction digits which will give you these lines you have to add before formatting your number:
numberFormatter.minimumFractionDigits = 1
numberFormatter.maximumFractionDigits = 2
Also notice, your input has to be 0.085 to get 8.5%. This is caused by the multiplier property, which is for percent style set to 100 by default.

Cast an int to a Float in Swift

Im trying to cast the results of a calculation (ShotPercentage) to a Float and present the results in the App as a 89 percent for example. But I am struggling with the type casting any help would be greatly appreciated. Here is my code:
// calculate shot percentage
shotPercentage = makeCounter / totalCounter
shotPercentageLabel.text = "\(shotPercentage)"
You can apply a conversion to your Floatresult like this.
shotPercentage = Int(makeCounter / totalCounter)
var shotPercentageInt: Int
shotPercentageInt = 3/5
println("\(shotPercentageInt*100)%") // 0% because 3/5 = 0.6 -> Int = 0
//
var shotPercentageFloat: Float
shotPercentageFloat = 3/5
println("\(shotPercentageFloat*100)%") // 60.0% because 3/5 = 0.6 -> Float = 60%
// Convert Float to Int
var shotPercentageFloatToInt: Int
shotPercentageFloatToInt = Int(shotPercentageFloat)
println("\(shotPercentageFloat)") // 0.6
// It's amazing
You might want to use NSNumberFormatter. It does well going to strings and coming from strings. Regardless, it would look like:
let makeCounter = 15
let totalCounter = 20
let shotPercentage:Float = makeCounter / totalCounter
let formatter = NSNumberFormatter()
formatter.numberStyle = .PercentStyle
if let percentString = formatter.stringFromNumber(shotPercentage) {
shotPercentageLabel.text = percentString
}
In this case the label would read 75%, as the formatter will include the percent sign for you.

How to use println in Swift to format number

When logging-out a float in Objective-C you can do the following to limit your output to only 2 decimal places:
float avgTemp = 66.844322156
NSLog (#"average temp. = %.2f", avgTemp);
But how do you do this in Swift?
And how do you escape other characters in println in Swift?
Here's a regular Swift println statement:
println ("Avg. temp = \(avgTemp)")
So how do you limit decimal places?
Also, how do you escape double-quotes in println?
Here's the shortest solution I found thus far:
let avgTemp = 66.844322156
println(NSString(format:"%.2f", avgTemp))
Its like the swift version of NSString's stringWithFormat
Everything about the format of a number as a string can be adjusted using a NSNumberFormatter:
let nf = NSNumberFormatter()
nf.numberStyle = NSNumberFormatterStyle.DecimalStyle
nf.maximumFractionDigits = 2
println(nf.stringFromNumber(0.33333)) // prints 0.33
You can escape quotes with a backslash
println("\"God is dead\" -Nietzsche")
Println() is deprecated.
var avgTemp = 66.844322156
print("average temp. = (round(avgTemp*100)/100)") // average temp. = 66.84
//or
print(NSString(format:"average temp. = %.2f", avgTemp))) // average temp. = 66.84
avgTemp = 66.846322156
print(String(format:"average temp. = %.2f", avgTemp)) // average temp. = 66.85
If you need to print floating point numbers often with a certain precision, you could extend Float and Double with convenience methods. For example, for 2 significant figure precision:
// get Float or Double with 2 significant figure precision
var numberFormatter = NSNumberFormatter()
extension Float {
var sf2:String {
get {
numberFormatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
numberFormatter.maximumSignificantDigits = 2
return numberFormatter.stringFromNumber(self)!
}
}
}
extension Double {
var sf2:String {
get {
numberFormatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
numberFormatter.maximumSignificantDigits = 2
return numberFormatter.stringFromNumber(self)!
}
}
}
Then when you need to print things:
let x = 5.23325
print("The value of x is \(x.sf2)")