I've been reading the Apple Developer Documentation and it appears that it's not updated for the class NumberFormatter, they say it swapped from NSNumberFormatter to just NumberFormatter.
I've found a few examples of functionalities of this class in Swift 3 but I couldn't find how to set the maximumFractionDigits.
When I have a Double like this 0.123456789, I'd like to convert it into a String with just 4 fractional digits for example, like this 0.1234.
If you don't want it to round up, but rather always round down, use .floor or .down:
let foo = 0.123456789
let formatter = NumberFormatter()
formatter.maximumFractionDigits = 4
formatter.roundingMode = .down
let string = formatter.string(from: NSNumber(value: foo))
If you want the traditional rounding format, just omit the .roundingMode, and this will result in "0.1235".
For more information, see the NumberFormatter reference documentation.
Related
I'm trying to kern Persian numbers but .kerning() doesn't work.
Do you know any another way to set kern in persian numbers?
Text(250000, format: .number)
.kerning(5)
Text("250000")
.kerning(5)
Text(25000.asCurrencyFa)
.kerning(5)
This is my extension how I format the numbers:
extension Int {
var asCurrencyFa: String {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = Locale(identifier: "fa_IR")
formatter.currencySymbol = ""
return formatter.string(from: self as NSNumber)!
}
}
Kerning (really "expansion" here since this isn't really kerning IMO) is a function of the font, and the default system font (.SFUI-Regular, but I'm pretty sure it's actually SF Arabic) does not appear to support expanding Persian digits. It will expand Persian letters somewhat. For example, فارسی, with .kern(10) displays this way vs "normal."
That said, there are fonts that support expanding Persian digits. For example, Geeza Pro:
let ctFont = CTFontCreateWithName("GeezaPro" as CFString, 16, nil)
...
Text(25000.asCurrencyFa)
.font(Font(ctFont))
Text(25000.asCurrencyFa)
.font(Font(ctFont))
.kerning(10)
One approach to get the "look" is to add HAIR SPACE U+200A between all the characters:
return formatter.string(from: self as NSNumber)!
.map(String.init).joined(separator: "\u{200A}")
This will be a little wider than expected because the HAIR SPACE has width of its own, plus there are two "kernings" per digit pair rather than one. But I do believe it will work with every font. I tried using various zero-width spaces, but they're mostly ignored, except around the comma.
I have this line:
randomIndex = Int(drand48() % Double(alphabetColors.count))
And Xcode 8 (Swift 3) tells me:
'%' is unavailable: Use truncatingRemainder instead
Is there no operator anymore? How should I convert my code?
You can simply follow the diagnostic message:
let randomIndex = Int(drand48().truncatingRemainder(dividingBy: Double(alphabetColors.count)))
Or using arc4random_uniform(_:) would be a better alternative.
let randomIndex = Int(arc4random_uniform(UInt32(alphabetColors.count)))
This seems to be available for me, currently on Swift 3.1, so possible it was added back.
My guess is that it's somewhere in Foundation and needs an explicit import Foundation
Update
This is for Int types only. It seems that for doubles, truncating remainder is required.
Cast your variable to Int before mod
example
let result = Int(value) % 3
As per new Guideline Swift 5, it's changed
value.truncatingRemainder(dividingBy: 2)
Use https://developer.apple.com/documentation/swift/double/2884269-remainder
If you use truncatingRemainder (as mentioned in the other comments) then it is going to floor the value first, which means 3.14 is going to become 3.
You can use this code to find the modulus of 2 numbers.
label.text is to display the result in a label that I have already designed
let result = Int(num1) % Int(num2)
label.text = String(Int(result))
At the moment I'm converting my project to Swift 3.
I have a code block like this:
let someString = "asd.asABCDEFG.HI"
let regexp = "^\\w*[.]\\w{2}"
let range = someString.rangeOfString(regexp, options: .RegularExpressionSearch)
let result = someString.substringWithRange(range!)
The rangeOfString method is gone in Swift 3. Can somebody post an example, how a regexp search can be done. Thanks in advance.
In Swift 3 rangeOfString is like range(of:options:) and substringWithRange is like substring(with:).
if let range = someString.range(of:regexp, options: .regularExpression) {
let result = someString.substring(with:range)
}
I am new in swift, I have been working with it only few weeks and now I am trying to parse something like a price list from incoming string. It has the next format:
2.99 X 3.00 = 10 A
Some text here
1.22 X 1.5 10 A
And the hardest part is that sometime A or some digit is missing but X should be in the place.
I would like to find out how it is possible to use regex in swift (or something like that if it does not exist) to write a template for parsing the next value
d.dd X d.d SomeValueIfExists
I would very appreciate any useful information, topics to read or any other resources to get more knowledge about swift.
PS. I have access to the dev. forums but I've never used them before.
I did an example recentl, and maybe a little harder than necessary, to demonstrate RegEx use in Swift:
let str1: NSString = "I run 12 miles"
let str2 = "I run 12 miles"
let match = str1.rangeOfString("\\d+", options: .RegularExpressionSearch)
let finalStr = str1.substringWithRange(match).toInt()
let n: Double = 2.2*Double(finalStr!)
let newStr = str2.stringByReplacingOccurrencesOfString("\\d+", withString: "\(n)", options: NSStringCompareOptions.RegularExpressionSearch, range: nil)
println(newStr) //I run 26.4 miles
Two of these have "RegularExpressionSearch". If you put this in a playground you can see what each line does. Note the double \ escapes. One for the normal RegEx use and anther because \ is a special character in Swift.
Also a good article:
http://benscheirman.com/2014/06/regex-in-swift/
I have been messing with this for a while now, and decided to post on here to see if anyone could help out. I even messed around with the RegExr tool (with no luck):
http://gskinner.com/RegExr
Anyway, I have a String that contains the verbiage (without the quotes):
"13.5 to 14.1"
I need to create a var with the first number: 13.5 and a var with the second number: 14.1
So I want the following result:
var firstVal:String = 13.5;
var secondVal:String = 14.1;
I got it to work by doing the following for the first number:
var lowRegExp:RegExp=/\d[0-9].\d[0-9]/;
And for the second number I did this:
var highRegExp:RegExp=/\d[0-9].\d[0-9]$/;
My problem here is that I will not know the format of the String. It could also look like this (two digits trailing the decimal):
13.57 to 14.10
So I need to make sure that it works using the following combinations:
13.50 to 14.1, 13.5 to 14.10, 3.50 to 4.10, 3.5 to 4.1 (all combinations must work)
Any help is much appreciated!
Here is what I got to work. I am not sure how clean this is, and I am not a fan of hard coding, but it works for all scenarios. If someone knows a clean way to do this, please let me know.
var myString:String="13.5 to 14.1";
var firstVal:String=myString.substring(0, myString.search(" to "));
var secondVal:String=myString.substring((myString.search(" to ") + 4));
Should be pretty straight forward, you want the following:
- Any # of digits, followed by a period literal, followed by any # of digits.
Pattern: \d+\.\d+
So use something similar:
var mystr:String = "15.4 to 153.93";
var tokens:Array = mystr.match(/\d+\.\d+/g);
Also, I have gotten myself in the habit of using regexpal.com which is way faster than iterative testing in your application. ;)