Why does JSTL parseNumber parse 1000.0 to 10000? - parsing

I have an input form where I want to insert some numbers and calculate some results. So my input field looks like
<fmt:parseNumber var = "a" type = "number"
value = "${object.someAttribute}" integerOnly = "true"/>
<input type="number" name="someAttribute" required pattern="[0-9]" value="${a}" />
I want to do following: at first visit, a user should insert a number (Integer). In the calculation, all values are Double to prevent casting side effects. When the site is refreshed / the user wants to repeat the calculation, the input field should be preset with the recently used value. Therefore I tried fmt:parseNumber to parse the Double value from the object to an Integer.
At first try I omitted integerOnly = "true" but got an Error (as '1000.0' is not a valid input, that's understandable as I specified pattern="[0-9]").
But with integerOnly set, it changes the value from 1000.0 to 10000. What am I doing wrong? How could I parse it to achieve my goal?

If you are using EL 2.2+, you can simply convert a double to integer by invoking a non-getter method on your Double object:
${yourDouble.intValue()}
See:
https://stackoverflow.com/tags/el/info

Related

Minecraft Computercraft Rednet.broadcast() isn't passing the proper value

The full code itself is very convoluted, long, and novice-written; I'm trying to send x and z coordinates via rednet from a computer to a separate receiver turtle.
Receiver
rednet.broadcast("Awaiting Input!")
xAxis = rednet.receive() --Have tried tonumber(rednet.receive()) on both same result
zAxis = rednet.receive()
rednet.broadcast(xAxis) --Both values return 2 regardless of what I enter in the sender.
rednet.broadcast(zAxis)
sender
if(irrelevant == "genericstringhere") then
print(rednet.recieve()) --Awaiting Input!
io.write("X Axis: ") --Pizzaz
message = io.read() --Have tried using terms like X and xAxis.
rednet.broadcast(message) --Broadcast whatever tf I typed in.
sleep(0.3)
io.write("Z Axis: ") --More Pizzaz
message = io.read() --Have tried using terms like Z or zAxis
rednet.broadcast(message) --Broadcast whatever tf I typed in. again.
print(rednet.receive()) --Receive xAxis value from sender.
print(rednet.receive()) --Receive zAxis value from sender.
end
Both results at the end of execution return 2 instead of the value I input in io.read(). I have tried tonumber(), tostring(), and every combination of the two, and I can't seem to get it to work.
Per the documentation for rednet.receive:
Returns number senderID, any message, string protocol
The 2 you're seeing is the sender ID, not the message. Instead of xAxis = rednet.receive(), do senderId, xAxis = rednet.receive(), and similarly for everywhere else you assign its value. (If you were wondering why print showed you numbers before the messages, that's why.)

Pass an empty value to an OData V2 Edm.Time property

I have a variable type time but sometimes this variable doesn't have anything.
When it is initial, it shouldn't be "000000", I want an empty value without anything (no zeros). Let me explain my problem with the code:
IF lwa_hora IS INITIAL.
CLEAR lwa_hora.
ls_entity-hora = lwa_hora. " Result: 000000 but I don't want any zero
ELSE.
ls_entity-hora = lwa_hora. " Result: 000000
ENDIF.
I tried with CLEAR but nothing happens.
I need this is because in the JavaScript frontend client logic, I need the OData property to contain a falsy value (E.g. null or an empty string "").
But it always has the value "000000" which is not an empty string. Is it possible to make something in the backend to "clear" the variable?
The time data-type in abap (t) is a value-type. It's internally implemented as an integer counting the seconds since midnight. 0 seconds since midnight is a valid value, so it can't have a null-value.
However, ABAP allows you to create a reference to any value-type:
hora TYPE REF TO t.
That means hora will be a reference to a variable of TYPE t. Initially, this reference will be unbound, which is conceptually very similar to a null-reference in other programming languages. You can check that with:
IF ls_entity-hora IS BOUND.
...
IF ls_entity-hora IS NOT BOUND.
You can assign a time value with GET REFERENCE OF lwa_hora INTO ls_entity-hora. But now you have a reference to an existing variable. Change the value of lwa_hora, and the value of ls_entity-hora also changes. That might not always be what you want. So it might be better to create a new piece of data in memory for our reference to point to:
CREATE DATA ls_entity-hora.
Now ls_entity-hora is no longer unbound (or "null" if you want to call it that), it points to a new time-value of 000000. If you want to read or change the value of this nameless piece of data this reference points to, then you can do this with the dereferencing-operator ->*:
ls_entity-hora->* = lwa_hora.
If you intentionally want to set a reference to unbound (or "set the reference to null"), you can do that by clearing the reference:
CLEAR ls_entity-hora.
By the way: Representing a point in time by two separate variables of the types d and t fell out of fashion in the past decade. The current best practice for this situation is to use a single variable of type TIMESTAMP (if you need second precision) or TIMESTAMPL (if you need microsecond precision). A timestamp of 00000000000000 is obviously an illegal value, so it can be used to represent the absence of a point in time. This type also usually makes it much easier to communicate with a SAPUI5 frontend (like in your case), because many of the technologies for making oData services offer automatic conversion between Javascript Date and ABAP TIMESTAMP.
An alternative to heap allocating the time would be to store a boolean next to it, indicating whether it is set or not:
TYPES:
BEGIN OF optional_time,
time TYPE t,
is_null TYPE abap_bool,
END OF optional_time.
DATA(no_time) = VALUE optional_time( is_null = abap_true ).
" Setting to null:
DATA(some_time) = no_time.
" Setting to some time:
some_time = VALUE #( time = '12:30' ).
IF some_time = no_time.
" ...
ENDIF.
This sort of things is probably better to handle on front-end than on back-end.
SAP Gateway serializes ABAP Date/time initial values to NULL in the OData response if the corresponding property is nullable. You need to make sure this property is set to TRUE like in this sample
you can also set this property in runtime
TRY .
lo_action = model->get_entity_type( iv_entity_name = 'Z_REPORTType').
lo_property = lo_action->get_property( iv_property_name = 'Requested_Date').
lo_property->set_nullable( iv_nullable = abap_true ).
CATCH /iwbep/cx_mgw_busi_exception /iwbep/cx_mgw_med_exception /iwbep/cx_mgw_tech_exception INTO DATA(lo_exception).
ENDTRY.

Can a DOORS real-number attribute be assigned a nonfinite value?

I've got a DOORS module with attributes that are filled with number strings, because the attributes are not applicable to all objects. To identify unambiuously the objects which aren't affected, the attributes currently contain "N/A" . For purposes of sorting and other operations, I would like to replace these attributes with numeric types. Is there any special value that DOORS understands as "NULL" or "N/A", or even 'Inf' that can be used in a numeric attribute?
I know I could do some scripting to create separate DXL attributes which convert the number strings to numeric but would rather avoid that if possible.
I fear that you will either have to stick to strings or define your personal integer value which represents 'n/a', for example '0'. Objects with the '0' value will be easy to treat after sorting -- with DXL you may use the bool null(t value) function which checks if value is null (bool: false, char: '0', int: 0, real: 0.0, string: ""). If you stick to integer strings, there is the function bool isValidInt(string s) which would help you to treat the "n/a" string accordingly.
Could you just leave the attribute empty for that object?
It seems like an int type attribute can be set to "" - and you could filter the objects based on that attribute being empty or non-empty.
Interestingly, in DXL space, the following seems to work for an integer based attribute ( inttest )
Object o = current
int x = o."inttest"
print ( x ) "\n"
print ( null x ) "\n"
print ( x == 0 )
Prints the following results:
0
true
true
So nulls are interpreted into 0 - something to be aware of if 0 is an expected value in your attributes.

Int Variable won't let me add data from UI text field

I'm currently working on making a savings app in Xcode 10. I'm working on a feature that lets users add the amount of money they have saved for something into the app through a UI text field. I can't find a way to return the text from the text field to an Integer and add that to the total sum of money that has been saved. Also whenever I tried to add a test variable I got plenty of errors.
var amountSavedSoFar += amountOfMoneySaved
Both I have set to be integers. I'm trying to set amountOfMoneySaved equal to the numbers in the text field, but it doesn't seem to work.
'+=' is not a prefix unary operator
Consecutive statements on a line must be separated by ';'
Type annotation missing in pattern
Unary operator cannot be separated from its operand
You've got a few issues as you mentioned:
amountSavedSoFar is declared in the saveAmount function and will not be persisted if you call that function more than once.
amountSaved.text is not being converted from String to the appropriate type (Int, Double, etc.)
amountSavedSoFar isn't typed or initialized.
Try something like:
var amountSavedSoFar: Int = 0
#IBAction func saveAmount(_ sender: Any) {
//Convert the text and default to zero if conversion fails
amountSavedSoFar += Int(amountSaved.text) ?? 0
}

cannot be applied to operands of type 'UITextField' and 'Int'

I am trying to populate a Label with a text field input * 365
I keep getting the message:
Binary operator '*' cannot be applied to operands of type 'UITextField' and 'Int'
var hours = (hoursTextField.text as NSString).doubleValue
var hoursInAYear = hoursTextField * 365
Your first line is calculating the doubleValue of what's entered into the text field, but you're not using that hours variable. Perhaps you want:
var hoursInAYear = hours * 365
The warning you are getting is telling you that you're trying to use the * operator between a variable whose type is UITextField and another variable whose type is Int (this is what your 365 literal is interpreted as).
This warning will come up any time we try to use an operator between two types for which the operator does not have an overload. It is particularly common when one of our operand's types is implicitly determined because we're using a literal somewhere. To resolve the issue, we must double check our instantiation of our operands and be sure they're of types for which our operator has an overload.
If they are not, then we should either change how we create these variables so they have the right type, or find some way of converting them when we use them with the operator.
When we change our mistaken variable from the text field to the double we just calculated, Swift is able to calculate this correctly. Despite previously claiming that 365 was an Int, being a literal, it can be interpreted as several different types, one of which includes Double.
When we attempt to use the * between a variable of type Double and a literal number, the literal number will be correctly converted to a Double, and we'll use the overload of the * operator which accepts two doubles (and returns a double).
You're trying to multiply hoursTextField by 365. Did you mean to write:
var hours = (hoursTextField.text as NSString).doubleValue
var hoursInAYear = hours * 365 // hours, not hoursTextField.
I think it is basically just a typo or copy-paste-mistake of yours since you already calculate the hours variable correctly and dont use it afterwards. Simply change your second line to
var hoursInAYear = hours * 365

Resources