Expected '33990Times Jobs' to be greater than 0 - protractor

My code:
var listItemText = element.all(by.css(".list-group-item.ng-binding.ngscope")).get(0).getText()
.then(function(text){ return text.replace(/[\r\n]/g, "")
})
expect(listItemText).toBeGreaterThan(0);
How to compare if string is number with texts should be greater than 0 in protractor.

If you want to convert String to a Number you can either use parseFlaot for floating point numbers or parseInt for integers. As long as it will not start with letter characters it will cut off the non number part. Also watch out with parsing the number as it will also cut of leading 0s in front of a number - you might want to improve your regexp to grab only the number from where you expect it to be in the string to make it more bulletproof.
Also you don't need to use element.all(locator).get(0), element(locator) for multiple occurrences will always return first element found.
element(by.css(".list-group-item.ng-binding.ngscope")).getText()
.then(function(text){
var listItemText = text.replace(/[\r\n]/g, "");
expect(praseFloat(listItemText)).toBeGreaterThan(0);
});

Expected '33990Times Jobs' to be greater than 0
First of all, you are comparing a string with a number. And, the string itself contains the extra Times Jobs part. Let's extract all the digits from the text and use parseInt to convert a string to an integer:
var listItemText = element.all(by.css(".list-group-item.ng-binding.ngscope")).first().getText().then(function(text) {
return parseInt(text.match(/\d+/)[0]);
});
expect(listItemText).toBeGreaterThan(0);

Related

How to align numbers vertically? Swift, UIKit

I want to align numbers by digits in table rows. For example:
___123.4
-5 678.9
That is, so that tens are under tens, units under units, a fractional number under a fractional number.
To convert a Number to a String, I use the numberStringFormatter function.
func numberStringFormat(_ number: Double) -> String {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.maximumFractionDigits = 1
numberFormatter.groupingSeparator = " "
let result = numberFormatter.string(from: NSNumber(value: number))
return result ?? ""
}
This function sets the fractional format, determines the number of decimal places, and groups the numbers before the decimal point by digits.
But if the number is an integer, without fractions, or a digit after a floating point after rounding it turns out to be 0, then the formatted string looks like this, for example, 123
And then these numbers in the rows of the table are shifted and it turns out like this:
----123
5 678.9
That is, the fractional number on the bottom row is under the integer number on the top row.
In my opinion, I can solve this task if I force the Number to always show 0 after converting to a String.
I tried googling but couldn't find an answer to this question.
Maybe someone has already encountered such situations and can suggest a possible solution, or at least in what direction to move?
Or, perhaps there is some other solution without forcing 0 to be shown, but simply aligning the characters vertically?
Any ideas are welcome. I really appreciate your help.
Update: A greate solution from HangarRash.
minimumFractionDigits = 1

Swift Dictionary is slow?

Situation: I was solving LeetCode 3. Longest Substring Without Repeating Characters, when I use the Dictionary using Swift the result is Time Limit Exceeded that failed to last test case, but using the same notion of code with C++ it acctually passed with runtime just fine. I thought in swift Dictionary is same thing as UnorderdMap.
Some research: I found some resources said use NSDictionary over regular one but it requires reference type instead of Int or Character etc.
Expected result: fast performance in accessing Dictionary in Swift
Question: I know there are better answer for the question, but the main goal here is Is there a effiencient to access and write to Dictionary or someting we can use to substitude.
func lengthOfLongestSubstring(_ s: String) -> Int {
var window:[Character:Int] = [:] //swift dictionary is kind of slow?
let array = Array(s)
var res = 0
var left = 0, right = 0
while right < s.count {
let rightChar = array[right]
right += 1
window[rightChar, default: 0] += 1
while window[rightChar]! > 1 {
let leftChar = array[left]
window[leftChar, default: 0] -= 1
left += 1
}
res = max(res, right - left)
}
return res
}
Because complexity of count in String is O(n), so that you should save count in a variable. You can read at chapter
Strings and Characters in Swift Book
Extended grapheme clusters can be composed of multiple Unicode scalars. This means that different characters—and different representations of the same character—can require different amounts of memory to store. Because of this, characters in Swift don’t each take up the same amount of memory within a string’s representation. As a result, the number of characters in a string can’t be calculated without iterating through the string to determine its extended grapheme cluster boundaries. If you are working with particularly long string values, be aware that the count property must iterate over the Unicode scalars in the entire string in order to determine the characters for that string.
The count of the characters returned by the count property isn’t always the same as the length property of an NSString that contains the same characters. The length of an NSString is based on the number of 16-bit code units within the string’s UTF-16 representation and not the number of Unicode extended grapheme clusters within the string.

Number validation and formatting

I want to format, in real time, the number entered into a UITextField. Depending on the field, the number may be an integer or a double, may be positive or negative.
Integers are easy (see below).
Doubles should be displayed exactly as the user enters with three possible exceptions:
If the user begins with a decimal separator, or a negative sign followed by a decimal separator, insert a leading zero:
"." becomes "0."
"-." becomes "-0."
Remove any "excess" leading zeros if the user deletes a decimal point:
If the number is "0.00023" and the decimal point is deleted, the number should become "23".
Do not allow a leading zero if the next character is not a decimal separator:
"03" becomes "3".
Long story short, one and only one leading zero, no trailing zeros.
It seemed like the easiest idea was to convert the (already validated) string to a number then use format specifiers. I've scoured:
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html
and
http://www.cplusplus.com/reference/cstdio/printf/
and others but can't figure out how to format a double so that it does not add a decimal when there are no digits after it, or any trailing zeros. For example:
x = 23.0
print (String(format: "%f", x))
//output is 23.000000
//I want 23
x = 23.45
print (String(format: "%f", x))
//output is 23.450000
//I want 23.45
On How to create a string with format?, I found this gem:
var str = "\(INT_VALUE) , \(FLOAT_VALUE) , \(DOUBLE_VALUE), \(STRING_VALUE)"
print(str)
It works perfectly for integers (why I said integers are easy above), but for doubles it appends a ".0" onto the first character the user enters. (It does work perfectly in Playground, but not my program (why???).
Will I have to resort to counting the number of digits before and after the decimal separator and inserting them into a format specifier? (And if so, how do I count those? I know how to create the format specifier.) Or is there a really simple way or a quick fix to use that one-liner above?
Thanks!
Turned out to be simple without using NumberFormatter (which I'm not so sure would really have accomplished what I want without a LOT more work).
let decimalSeparator = NSLocale.current.decimalSeparator! as String
var tempStr: String = textField.text
var i: Int = tempStr.count
//remove leading zeros for positive numbers (integer or real)
if i > 1 {
while (tempStr[0] == "0" && tempStr[1] != decimalSeparator[0] ) {
tempStr.remove(at: tempStr.startIndex)
i = i - 1
if i < 2 {
break
}
}
}
//remove leading zeros for negative numbers (integer or real)
if i > 2 {
while (tempStr[0] == "-" && tempStr[1] == "0") && tempStr[2] != decimalSeparator[0] {
tempStr.remove(at: tempStr.index(tempStr.startIndex, offsetBy: 1))
i = i - 1
if i < 3 {
break
}
}
}
Using the following extension to subscript the string:
extension String {
subscript (i: Int) -> Character {
return self[index(startIndex, offsetBy: i)]
}
}

Removing Digits from a Number

Does anybody know if there is a way of removing (trimming) the last two digits or first two digits from a number. I have the number 4131 and I want to separate that into 41 and 31, but after searching the Internet, I've only managed to find how to remove characters from a string, not a number. I tried converting my number to a string, then removing characters, and then converting it back to a number, but I keep receiving errors.
I believe I will be able to receive the first two digit by dividing the number by 100 and then rounding the number down, but I don't have an idea of how to get the last two digits?
Does anybody know the function to use to achieve what I'm trying to do, or can anybody point me in the right direction.
Try this:
var num = 1234
var first = num/100
var last = num%100
The playground's output is what you need.
You can use below methods to find the last two digits
func getLatTwoDigits(number : Int) -> Int {
return number%100; //4131%100 = 31
}
func getFirstTwoDigits(number : Int) -> Int {
return number/100; //4131/100 = 41
}
To find the first two digit you might need to change the logic on the basis of face value of number. Below method is generalise method to find each digit of a number.
func printDigits(number : Int) {
var num = number
while num > 0 {
var digit = num % 10 //get the last digit
println("\(digit)")
num = num / 10 //remove the last digit
}
}

Put commas in a number string?

Given a number, I'd like to transform it into a string, but insert commas in the thousands place etc, like:
int number = 123456;
String formatted = String.valueOf(number);
println(formatted); // but print "123,456"?
does GWT offer a way of doing this, or should we write our own method?
Thanks
The first one is to format a number with decimal points and include a comma. The other is with out decimal points. I'm giving this out because it wasn't so easy for me to figure it out the first time when I was starting out.
private NumberFormat decFormat = NumberFormat.getFormat("#,##0.00;(#,##0.00)");
private NumberFormat intFormat = NumberFormat.getFormat("#,##0;(#,##0)");
Use NumberFormat provided by GWT.