I have a question. I write tests for a mobile app. And at this moment, I compare the prices in different currencies like Pound, Euro, India, etc. A possible comparison should be "now £1.678,95". It's no problem for me to cut the "now", to cut the whitespace - short, to get the string in a possible Int or Double formation. BUT now I'm in France. In France the formation is "maintenant 2 500,00 €". No problem with the "maintenant", no problem with the whitespace outside the price and no problem with "€".
BUT there is a space IN the price between 2 and 500. If I run my test, I have only the "2" the rest is gone! How can I do it, that the whitespace should not cut here. It should backspaced from "2 500,00" to "2500,00".
Hope you have an idea :) Thanks!
My code at this moment is:
var firstPrice = XCUIApplication().collectionViews.cells.element(boundBy: 0).staticTexts.element(boundBy: 2).label
firstPrice = firstPrice.replacingOccurrences(of: "£", with: "")
firstPrice = firstPrice.replacingOccurrences(of: "€", with: "")
firstPrice = firstPrice.replacingOccurrences(of: "₹", with: "")
let firstPriceArray = firstPrice.components(separatedBy: .whitespaces).filter { !$0.isEmpty }
firstPrice = firstPriceArray[1]
let firstPriceTrimmedDouble = firstPrice.toDouble(with: SiteIDHelper.locale(from: SiteIDHelper.SiteID(rawValue: Int(sideIDS))!))!
print(firstPriceTrimmedDouble)
Tough, because you don't know the locale of the string. 1234 Euro can be written as € 1,234.00
in English style, or 1.234,00 €
in French and German styles. (Euro is very common in England too).
From the limited examples you provided, you can remove the first word, then deleting all spaces, commas, dots and currency signs from the remaining before converting it to Double:
let priceString = "maintenant 2 500,00 €"
let unwanted = " ,.£€₹"
var doubleValue : Double?
if let range = priceString.range(of: " ") {
let chars = priceString[range.upperBound..<priceString.endIndex]
.characters.filter({ !unwanted.characters.contains($0) })
doubleValue = Double(String(chars))
}
// run your asserts here