Checking and returning numeric field value? - xpages

To check whether value is numeric in LS, I would write the following code:
Dim strVal as String
Dim dVal as Double
strVal = doc.fldValue(0)
If IsNumeric(strVal) Then
dVal = cdbl(strVal)
End If
How would we achieve this within XPages? I just tried getItemValueString on a numeric field and it didn't work. getItemValueDouble worked, but as we all know we cannot rely on that field value to be numeric (in case some rogue agent is ran and converts a field on a particular document to be a text field by mistake!), so how do we check whether it's numeric or not in XPages? Is there an easy way to achieve this or do we have to use getItemValue and check through the vector object? Has anyone else noticed this?
Thanks.

In my experience when you are saving a numeric value it will be returned as a double value. If you want to be sure which datatype the .getitemvalue("field") value returns, you can use the .getClass() method on the returned value. for instance when you want to print it to the console:
var x = doc.getItemValue("fieldx").get(0)
if(x!=null){
print(x.getClass());
}
because of this you can use code like this:
var x = doc.getItemValue("fieldx").get(0) // get first value of the field
if(x != null){
if(x instanceof java.lang.Double){
}
if(x instanceof java.lang.Integer){}
}
or... you could just use
#IsNumber()
;)

Related

Lua string to table

I have a string that I need to read as a table
notes = "0,5,10,16"
so if I need the 3rd value of the current notes that is 10
value = notes[3]
If you trust the strings, you can reuse the Lua parser:
notes = "0,5,10,16"
notes = load("return {"..notes.."}")()
print(notes[3])
For the example string, you can just do
local notes_tab = {}
for note in notes:gmatch("%d*") do
table.insert(notes_tab, tonumber(note))
end
We can change the __index metamethod of all strings to return the nth element separated by commas. Doing this, however, gives the problem that we cannot do something like notes:gmatch(",?1,?") anymore. See this old StackOverflow post. It can be solved, by checking whether the __index is called with a string or other value.
notes = "0,5,10,16"
getmetatable("").__index = function(str, key)
if type(key) == "string" then
return string[key]
else
next_value = string.gmatch(str, "[^,]+")
for i=1, key - 1 do
next_value()
end
return next_value()
end
end
print(notes[3]) --> 10
string.gmatch returns a function over which we can iterate, so calling this n times will result in the nth number being returned.
The for loop makes sure that all the numbers before which we want have been iterated over by the gmatch.
Depending on what you want to do with the numbers you can either return it as a string or convert it to a number immediately.

Use String isEmpty to check for empty string

Swift Programming Language mentions using isEmpty to check for empty string. Are there cases where checking string against "" not yield the same result as using isEmpty?
In other words:
if str.isEmpty {
XCTAssert(str == "", "This should be true as well")
}
From the documentation:
Find out whether a String value is empty by checking its Boolean isEmpty property:
if emptyString.isEmpty {
print("Nothing to see here")
}
The empty string is the only empty string, so there should be no cases where string.isEmpty() does not return the same value as string == "". They may do so in different amounts of time and memory, of course. Whether they use different amounts of time and memory is an implementation detail not described, but isEmpty is the preferred way to check in Swift (as documented).

double.TryParse("Infinity",out dbl) return zero

I am using double.TryParse method to parse my string to double. Here in some case string might be NaN, Infinity, -Infinity. While parsing this kind of text I want double value as zero instead of double.Nan, double.Infinity. So, double.TryParse has any option to do so or need to write a method to filter this.
TryParse has no option to behave the way you desire so you will have to code it yourself. Given that Infinity and NaN are not zero, it can be no surprise that none of the built in methods return zero for those inputs.
You can parse it like that:
double value = 0;
double a = double.TryParse("YourString", out value) == true ? value : 0;
If its not double, you will get 0 else the value.

String Not Equal to Something Else

I am working on android eclipse. This seems an easy question but I haven't found an answer of it yet! I have a string and I want to include in my if statement if that is not equal with something else. But only I know is the mystring.equals("example"). How do I enter if that string is not equal to something else? Thanks a lot
How do I enter if that string is not equal to something else?
Try this guy !. Example
if (!"someString".equals(myString)){
This tells it that if the value of myString isn't someString then it will enter the if condition. Doing it this way instead of
if (!myString.equals("someString"){
will protect against NPE so if myString is null you won't get an exception in the first example.
To be more precise, the String.equals() function is a function of return type boolean.
This means that it either returns true or false based on the values of the compared strings.
So:
if(myString.equals("other string"))
is equivalent to:
if(myString.equals("other string") == true)
To test for the contrary, you want:
if(myString.equals("other string") == false)
or the equivalent, as codeMagic mentionned: if(!myString.equals("other string"))

Two String variables of equal value are not comparing to be equal (android)

I have two variables, one is a String array that is filled from a database, and the other is generated in the program, and I want to execute code if they are equal. I have verified that they have the same value, but the comparison seems to fail.
In this case, the element of the String array is also equal to "2", but the comparison fails.
if (r3.isChecked())
{
choosenButton = "2";
if (choosenButton == Global.dbCorrectAnswer[0])
{
Toast.makeText(MySchoolOnline.this, "Correct", Toast.LENGTH_SHORT).show();
}
}
You should use equals to compare the variables:
choosenButton.equals(Global.dbCorrectAnswer[0])

Resources