So I have a Double that gets calculated and varies in length each time (based on certain input). This Double is placed in to a String.
var doubleNumber = 30440.8734
var string = "€ \(round(100 * doubleNumber) / 100)"
var numberString: String = string.stringByReplacingOccurrencesOfString("€ ", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
var splitString = split(numberString) {$0 == "."}
println(splitString[0])
Result would be: "30440"
What I would like to do is to place spaces in this number for readability. For which the result would end in: "30 440"
Any suggestions?
Regardless of the issue of currency formats, you can use an NSNumberFormatter to add spaces as grouping separators in a number:
var doubleNumber = 30440.8734
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = .CurrencyStyle
numberFormatter.currencySymbol = "€ "
numberFormatter.currencyGroupingSeparator = " "
numberFormatter.maximumFractionDigits = 0
if let formattedString = numberFormatter.stringFromNumber(doubleNumber) {
println(formattedString)
}
Output: € 30 441
I do think that NSNumberFormatter approach is better:
let f = NSNumberFormatter()
f.groupingSeparator = " "
f.groupingSize = 3
f.usesGroupingSeparator = true
f.stringFromNumber("30456".toInt()!)
but here's my solution if I would do this task without formatter:
var i: Int = count(number)
let a = "".join(map(number) {String($0) + (--i % 3 == 0 ? " " : "")})
a.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
Related
How to remove decimal point value if the number is more than 10.0. Below is the code what i have tried. At below code i am getting the value and i put the condition that if value is less than 1km then show number in meter, if value is more than 1 then show the number in km and if the value is greater than 10.0 then i am not able to remove the decimal point
let resultDelivery = String(format: "%.1f", Obj.distance)// Here getting value from api either i get 0.5 or 6.5 or 11.5
if (resultDelivery.starts(with: "0")){
let resultDelivery2 = String(format: "%.f", Obj.distance/1 * 1000)
cell.lblDeliverykm?.text = resultDelivery2.description + " " + "m".Localized() + " " + "" // result is 900 m
}
else if (resultDelivery.starts(with: "10.0")){
let resultDelivery2 = String(format: "%.0f", Obj.distance)
cell.lblDeliverykm?.text = resultDelivery2.description + " " + "km".Localized() + " " + "" // couldn’t able to remove decimal point
}
else {
cell.lblDeliverykm?.text = resultDelivery.description + " " + "km".Localized() + " " + "" // result is 8.6 km
}
Ah the joys of C-style formatting strings.
I present this as an alternative approach:
extension String.StringInterpolation
{
public mutating func appendInterpolation<F: BinaryFloatingPoint>(distance: F)
{
if distance < 1 {
appendLiteral("\(Int(distance * 1000))m")
}
else if distance >= 10 {
appendLiteral("\(Int(distance))km")
}
else
{
let d = (distance * 10).rounded(.toNearestOrEven) / 10
appendLiteral("\(d)km")
}
}
}
print("\(distance: 0.1)")
print("\(distance: 1)")
print("\(distance: 10)")
print("\(distance: 100)")
The output is
100m
1.0km
10km
100km
This will accept Double, Float, Float80, Float16 and any other type conforming to BinaryFloatingPoint.
If you want localizable formats, look into NumberFormatter.
[EDIT] as noted by #flanker in comments, LengthFormatter with its method, string(from: String, unit: LengthFormatter.Unit) -> String would be the way to go rather than NumberFormatter
How to display 12,4K with the variable NumberFollowers = 12458 in swift ?
The number displayed (12,4K) should change value as soon as NumberFollower changes value.
Example: if NumberFollowers is equal to 45875, the number displayed must be equal to 45,8K
PS: I have a problem with the 12,4K comma.
This is what i want to display :
1234 -> 1 234
12345 -> 12,3K
123456 -> 123K
1234567 -> 1,2M
12345678 -> 12,3M
123456789 -> 123M
Here is what I have
if NumberFollowers > 999999 {
NumberFollowersLabel.text = "\(NumberFollowers/1000000) M"
} else if NumberFollowers > 9999 {
NumberFollowersLabel.text = "\(NumberFollowers/1000) K"
} else {
NumberFollowersLabel.text = "\(NumberFollowers)"
}
This is what my code displays
1234 -> 1 234
12345 -> 12K
123456 -> 123K
1234567 -> 1M
12345678 -> 12M
123456789 -> 123M
Here is one way - based on the way you have shown how you want the values formatted:
func formatNumber(_ i: Int) -> String {
let nf = NumberFormatter()
nf.usesGroupingSeparator = true
nf.numberStyle = .decimal
var newNum: Double = 0
var numDec: Int = 0
var sPost: String = ""
switch i {
case 0..<10_000:
newNum = Double(i)
case 10_000..<100_000:
newNum = Double(i) / 1_000.0
numDec = 1
sPost = "K"
case 100_000..<1_000_000:
newNum = Double(i) / 1_000.0
sPost = "K"
case 1_000_000..<100_000_000:
newNum = Double(i) / 1_000_000.0
numDec = 1
sPost = "M"
default:
newNum = Double(i) / 1_000_000.0
sPost = "M"
}
nf.maximumFractionDigits = numDec
return (nf.string(for: newNum) ?? "0") + sPost
}
To check:
let a: [Int] = [
1234,
12345,
123456,
1234567,
12345678,
123456789
]
a.forEach { n in
print(formatNumber(n))
}
The Thousand and Decimal separators will be based on the default for the locale.
So, the debug output from that with a USA locale is:
1,234
12.3K
123K
1.2M
12.3M
123M
and, for example, with a Germany locale:
1.234
12,3K
123K
1,2M
12,3M
123M
I have this problem and all possible solutions I'm looking for online hasn't helped me. Is there a way to make a table like in a string format?
What I'm expecting the output will be like this for a string:
Category
ITEM1 QTY PRICE TOTAL
ITEM2 QTY PRICE TOTAL
but my output code is like this:
Category
ITEM1 QTY PRICE TOTAL
ITEM2 QTY PRICE TOTAL
Category
ITEM1 QTY PRICE TOTAL
ITEM2 QTY PRICE TOTAL
I figured out something that the item string length is the cost why they are not aligned formally in my format, but is there a way on how to solve this issue? I'm totally new in swift.
here is my code:
for category in self.categoryList {
sentence += "Category: \(category)\n"
for items in self.productList {
if category == items.category {
grandTotal += Double(items.qty)! * Double(items.price)!
let total: Double = Double(items.qty)! * Double(items.price)!
let item = (items.item as NSString).utf8String
let qty = (items.qty as NSString).utf8String
let price = (items.price as NSString).utf8String
sentence += String(format: "%-10s%20s%15s%15.02f\n", item!, qty!, price!, total)
}
}
sentence += "\n\n"
grandTotal = 0
}
lblData.text = sentence
You could pad your strings to a defined length:
let elements = ["ITEM1", "QTY", "PRICE", "TOTAL"]
// without padding
var sentence = ""
for e in elements {
if !sentence.isEmpty {
sentence += " "
}
sentence += e
}
print(sentence)
// prints "ITEM1 QTY PRICE TOTAL"
// with padding
sentence = ""
for e in elements {
sentence += e.padding(toLength: 10, withPad: " ", startingAt: 0)
}
print(sentence)
// prints "ITEM1 QTY PRICE TOTAL "
Or in a shorter form:
// without padding
var sentence = elements.joined(separator: " ")
print(sentence)
// with padding
sentence = elements.map({$0.padding(toLength: 10, withPad: " ", startingAt: 0)}).joined(separator: " ")
print(sentence)
I want to take input from user in binary, What I want is something like:
10101
11110
Then I need to perform bitwise OR on this. I know how to take input and how to perform bitwise OR, only I want to know is how to convert because what I am currently using is not giving right result. What I tried is as below:
let aBits: Int16 = Int16(a)! //a is String "10101"
let bBits: Int16 = Int16(b)! //b is String "11110"
let combinedbits = aBits | bBits
Edit: I don't need decimal to binary conversion with radix, as my string already have only 0 and 1
String can have upto 500 characters like:
1001101111101011011100101100100110111011111011000100111100111110111101011011011100111001100011111010
this is beyond Int limit, how to handle that in Swift?
Edit2 : As per vacawama 's answer, below code works great:
let maxAB = max(a.count, b.count)
let paddedA = String(repeating: "0", count: maxAB - a.count) + a
let paddedB = String(repeating: "0", count: maxAB - b.count) + b
let Str = String(zip(paddedA, paddedB).map({ $0 == ("0", "0") ? "0" : "1" }))
I can have array of upto 500 string and each string can have upto 500 characters. Then I have to get all possible pair and perform bitwise OR and count maximum number of 1's. Any idea to make above solution more efficient? Thank you
Since you need arbitrarily long binary numbers, do everything with strings.
This function first pads the two inputs to the same length, and then uses zip to pair the digits and map to compute the OR for each pair of characters. The resulting array of characters is converted back into a String with String().
func binaryOR(_ a: String, _ b: String) -> String {
let maxAB = max(a.count, b.count)
let paddedA = String(repeating: "0", count: maxAB - a.count) + a
let paddedB = String(repeating: "0", count: maxAB - b.count) + b
return String(zip(paddedA, paddedB).map({ $0 == ("0", "0") ? "0" : "1" }))
}
print(binaryOR("11", "1100")) // "1111"
print(binaryOR("1000", "0001")) // "1001"
I can have array of upto 500 string and each string can have upto 500
characters. Then I have to get all possible pair and perform bitwise
OR and count maximum number of 1's. Any idea to make above solution
more efficient?
You will have to do 500 * 499 / 2 (which is 124,750 comparisons). It is important to avoid unnecessary and/or repeated work.
I would recommend:
Do an initial pass to loop though your strings to find out the length of the largest one. Then pad all of your strings to this length. I would keep track of the original length of each string in a tiny stuct:
struct BinaryNumber {
var string: String // padded string
var length: Int // original length before padding
}
Modify the binaryOR function to take BinaryNumbers and return Int, the count of "1"s in the OR.
func binaryORcountOnes(_ a: BinaryNumber, _ b: BinaryNumber) -> Int {
let maxAB = max(a.length, b.length)
return zip(a.string.suffix(maxAB), b.string.suffix(maxAB)).reduce(0) { total, pair in return total + (pair == ("0", "0") ? 0 : 1) }
}
Note: The use of suffix helps the efficiency by only checking the digits that matter. If the original strings had length 2 and 3, then only the last 3 digits will be OR-ed even if they're padded to length 500.
Loop and compare all pairs of BinaryNumbers to find largest count of ones:
var numbers: [BinaryNumber] // This array was created in step 1
maxOnes = 0
for i in 0 ..< (numbers.count - 1) {
for j in (i + 1) ..< numbers.count {
let ones = binaryORcountOnes(numbers[i], numbers[j])
if ones > maxOnes {
maxOnes = ones
}
}
}
print("maxOnes = \(maxOnes)")
Additional idea for speedup
OR can't create more ones than were in the original two numbers, and the number of ones can't exceed the maximum length of either of the original two numbers. So, if you count the ones in each number when you are padding them and store that in your struct in a var ones: Int property, you can use that to see if you should even bother calling binaryORcountOnes:
maxOnes = 0
for i in 0 ..< (numbers.count - 1) {
for j in (i + 1) ..< numbers.count {
if maxOnes < min(numbers[i].ones + numbers[j].ones, numbers[i].length, numbers[j].length) {
let ones = binaryORcountOnes(numbers[i], numbers[j])
if ones > maxOnes {
maxOnes = ones
}
}
}
}
By the way, the length of the original string should really just be the minimum length that includes the highest order 1. So if the original string was "00101", then the length should be 3 because that is all you need to store "101".
let number = Int(a, radix: 2)
Radix helps using binary instead of decimical value
You can use radix for converting your string. Once converted, you can do a bitwise OR and then check the nonzeroBitCount to count the number of 1's
let a = Int("10101", radix: 2)!
let b = Int("11110", radix: 2)!
let bitwiseOR = a | b
let nonZero = bitwiseOR.nonzeroBitCount
As I already commented above "10101" is actually a String not a Binary so "10101" | "11110" will not calculate what you actually needed.
So what you need to do is convert both value in decimal then use bitwiseOR and convert the result back to in Binary String (in which format you have the data "11111" not 11111)
let a1 = Int("10101", radix: 2)!
let b1 = Int("11110", radix: 2)!
var result = 21 | 30
print(result)
Output: 31
Now convert it back to binary string
let binaryString = String(result, radix: 2)
print(binaryString)
Output: 11111
--: EDIT :--
I'm going to answer a basic example of how to calculate bitwiseOR as the question is specific for not use inbuilt function as string is very large to be converted into an Int.
Algorithm: 1|0 = 1, 1|1 = 1, 0|0 = 0, 0|1 = 1
So, What we do is to fetch all the characters from String one by one the will perform the | operation and append it to another String.
var str1 = "100101" // 37
var str2 = "10111" // 23
/// Result should be "110111" -> "55"
// #1. Make both string equal
let length1 = str1.characters.count
let length2 = str2.characters.count
if length1 != length2 {
let maxLength = max(length1, length2)
for index in 0..<maxLength {
if str1.characters.count < maxLength {
str1 = "0" + str1
}
if str2.characters.count < maxLength {
str2 = "0" + str2
}
}
}
// #2. Get the index and compare one by one in bitwise OR
// a) 1 - 0 = 1,
// b) 0 - 1 = 1,
// c) 1 - 1 = 1,
// d) 0 - 0 = 0
let length = max(str1.characters.count, str2.characters.count)
var newStr = ""
for index in 0..<length {
let charOf1 = Int(String(str1[str1.index(str1.startIndex, offsetBy: index)]))!
let charOf2 = Int(String(str2[str2.index(str2.startIndex, offsetBy: index)]))!
let orResult = charOf1 | charOf2
newStr.append("\(orResult)")
}
print(newStr)
Output: 110111 // 55
I would like to refer Understanding Bitwise Operators for more detail.
func addBinary(_ a: String, _ b: String) {
var result = ""
let arrA = Array(a)
let arrB = Array(b)
var lengthA = arrA.count - 1
var lengthB = arrB.count - 1
var sum = 0
while lengthA >= 0 || lengthB >= 0 || sum == 1 {
sum += (lengthA >= 0) ? Int(String(arrA[lengthA]))! : 0
sum += (lengthB >= 0) ? Int(String(arrB[lengthB]))! : 0
result = String((sum % 2)) + result
sum /= 2
lengthA -= 1
lengthB -= 1
}
print(result) }
addBinary("11", "1")
I have an array of timeStrings of the following format:
x Hr(s) xx min.
I need to add these up to give a total. I'm wondering what is the best way to do this?
My thoughts were to find the index of "Hr(s)". Then if i substring between index 0 and the index of "Hr(s)", I have my hrs var and then add 6 to index of "Hr(s)" and find the index of "min" - 1, to give me the min var.
Then I need to take into account if seconds is greater than 60. So if I divide my seconds var by 60 and the answer is great than 1, I add that answer to my hrs var?
Can anyone see any flaws in this logic?
Sample implementation:
JSON response:
{"status":"OK","hrs":[{"scheduleDate":"2015-11-09","userName":"John Doe","company":"Company X","hrsWorked":"0 Hr(s) 42 min"},{"scheduleDate":"2015-11-10","userName":"Jane Doe","company":"Company Y","hrsWorked":"0 Hr(s) 47 min"},{"scheduleDate":"2015-11-10","userName":"Bob loblaw","company":"Company X","hrsWorked":"0 Hr(s) 37 min"},{"scheduleDate":"2015-11-10","userName":"Joe Soap","company":"Company Y","hrsWorked":"0 Hr(s) 50 min"},{"scheduleDate":"2015-11-10","userName":"Test","company":"Company Y","hrsWorked":"0 Hr(s) 40 min"}],"queryStatus":"OK","message":null,"count":5}
var hrsVar = 0
var minsVar = 0
loop through hrsArray{
hrsMinStr = hrsWorkedInJson
if let endHrsIndex = hrsMinStr.lowercaseString.characters.indexOf("Hr(s)") {
print("Index: \(index)")
let hrsStr = Int(hrsMinStr.substringWithRange(Range<String.Index>(start: 0, end: endHrsIndex)))
hrsVar += hrsStr
let minStr = Int(hrsMinStr.substringWithRange(Range<String.Index>(start: endHrsIndex + 1, end: hrsMinStr.length - 3)))
minsVar += minStr
}
}
if minsVar/60 > 1 {
hrsVar = hrsVar + minsVar/60
minsVar = minsVar%60
}
Update
It seems as though I cannot pass in "Hr(s)" and instead only a single character "h". Because of this, I was trying to use the advancedBy(x) method to get the right endIndex. But I'm getting the error:
Cannot invoke initializer for type 'Range<Index>' with an argument list of type '(start: Int, end: String.CharacterView.Index)'
Updated code:
if let endHrsIndex = hrsMinStr.lowercaseString.characters.indexOf("h") {
print("Index: \(endHrsIndex)")
let hrsStr = Int(hrsMinStr.substringWithRange(Range<String.Index>(start: 0, end: endHrsIndex)))
hrsVar += hrsStr
let minStr = Int(hrsMinStr.substringWithRange(Range<String.Index>(start: endHrsIndex.advancedBy(5), end: hrsMinStr.length.characters.count.advancedBy(-3))))
minsVar += minStr
}
I'm really looking for the most efficient approach as possible, so please advise if there is a better way/if you see issues with this approach
import Foundation
let str = "0 Hr(s) 42 min"
let time = str
.stringByReplacingOccurrencesOfString("Hr(s)", withString:":")
.stringByReplacingOccurrencesOfString("min", withString: "")
.stringByReplacingOccurrencesOfString(" ", withString: "")
let t = time.characters.split(Character(":"))
let h = Int(String(t[0])) // 0
let m = Int(String(t[1])) // 1
and sum
import Foundation
var time: [(hours:Int,minutes:Int,seconds:Int)] = []
for i in 0...5 {
let h = Int(arc4random_uniform(24))
let m = Int(arc4random_uniform(60))
let s = Int(arc4random_uniform(60))
time.append((h,m,s))
}
let t = time.reduce(0) { (sum, time: (hours: Int, minutes: Int, seconds: Int)) -> Int in
sum + time.seconds + time.minutes * 60 + time.hours * 60 * 60
}
let seconds = t % 60
let minutes = ((t - seconds) / 60) % 60
let hours = ((t - seconds) / 3660)
time.forEach {
print(String(format: " %6d:%02d:%02d", arguments: [$0.hours,$0.minutes, $0.seconds]))
}
print("-------------------")
print(String(format: "Sum: %6d:%02d:%02d", arguments: [hours,minutes,seconds]))
/*
12:25:04
2:43:36
14:09:35
11:59:43
10:39:19
23:32:14
-------------------
Sum: 74:29:31
*/
You should most likely try using the scanners in your case.
let string = "12 Hr(s) 37 min"
let scanner = NSScanner(string: string)
var min: Int = 0
var hour : Int = 0
scanner.scanInteger(&hour)
scanner.scanString("Hr(s) ", intoString: nil)
scanner.scanInteger(&min)
Or even easier if you create this part in Objective-C:
int min, hour;
sscanf("12 Hr(s) 37 min", "%d Hr(s) %d min", &hour, &min);