OK.. Lets assume that we provided getParameterAsFloat(),
getParameterAsInt() methods, then in order to use them, we must know the
types of the parameters during the development time and it is not doing any
automatic type detection or casting. What it does is, it tries to cast the
param to the requested type within the function. In JavaScript, the way to
go with either parseFloat(),parseInt() or Number() function.
- parseFloat(param) - Converts param to a floating number
- parseInt(param) - Converts param to an integer. If you are using
parseInt, please always try to give radix as the second parameter. Else it
would make you confuse as parseInt("07") = 7, parseInt("08") = 0 and
parseInt("08", "10") = 8. The reason for this confusion is, parseInt
considers all string begin with 0 as numbers of base 8. Then as 8 is not a
valid number of base 8, it gives 0 as the result.
- Number(param) - This converts any string into JavaScript number either
int or float.
- Boolean(param) - This might not give what most of the expect too. i.e.
Boolean("true") = true, Boolean("false") = true, Boolean("null") = true.
Further, Boolean() function returns false only when the param equals "",
null, undefined or NaN. If you want to cast string "true" or "false" to
boolean, then there is not a build-in method except checking (param ==
"true") or (param == "false")
FYI.
On Tue, Mar 6, 2012 at 10:16 AM, Nuwan Bandara <[email protected]> wrote:
> Hi All,
>
> I dont think, we should think about the Java API when you are programming
> in Javascript. In JavaScript you have 4 premitive types (literals)
>
> 1. null
> 2. boolean
> 3. numaric
> 4. string
>
> And you have Objects as well for these with more operations associated see
> [1] so if you want to do numerical operations you have to cast your string
> to a Number and work on it. and thats it, an example is found at [2]. We
> cannot go on, providing operations such as *getParameterAsDouble*
> whenever we need some value to be casted, that's not Jaggery is for and
> thats not how you work with javascript.
>
> [1]
> http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
> [2]
> https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String#Distinction_between_string_primitives_and_String_objects
>
> Regards,
> /Nuwan
>
> On Tue, Mar 6, 2012 at 10:07 AM, Tharindu Mathew <[email protected]>wrote:
>
>> Request parameters are string. Its beyond the scope of jaggery to fix
>> this imo
>>
>> Regards,
>>
>> Tharindu
>> Sent from Transformer
>> On Mar 6, 2012 10:00 AM, "Hiranya Jayathilaka" <[email protected]> wrote:
>>
>>>
>>>
>>> On Tue, Mar 6, 2012 at 9:50 AM, Afkham Azeez <[email protected]> wrote:
>>>
>>>> I was also thinking the same. But since this is JavaScript, doesn't the
>>>> type conversion automatically happen? Perhaps not. How will it know whether
>>>> "1" is a String or int.
>>>
>>>
>>> That's correct. And I think it's ok too. This is consistent with
>>> JSP/Servlet API. If we are to solve this problem I think we should
>>> introduce more methods to the API such as getParameterAsInt and
>>> getParameterAsDouble.
>>>
>>> Thanks,
>>> Hiranya
>>>
>>>
>>>>
>>>>
>>>> On Tue, Mar 6, 2012 at 9:33 AM, Nuwan Bandara <[email protected]> wrote:
>>>>
>>>>> Hi Samisa,
>>>>>
>>>>> When you take the parameters from the request they are not type bound.
>>>>> they will be always String, and if you are sure that some parameter is an
>>>>> Integer you can cast it to a Integer and do math operations on it.
>>>>>
>>>>> It goes same for Java HTTPServletRequest [1]
>>>>>
>>>>> [1]
>>>>> http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameter(java.lang.String)
>>>>>
>>>>> Regards,
>>>>> /Nuwan
>>>>>
>>>>> On Tue, Mar 6, 2012 at 8:07 AM, Ruchira Wageesha <[email protected]>wrote:
>>>>>
>>>>>> I don't think it is possible to differentiate request parameters as
>>>>>> strings, integers, floats etc. i.e. when we pass something as a request
>>>>>> param from the client side, we don't consider their types.
>>>>>>
>>>>>> If I have understand your question properly, what is would be
>>>>>> the rationale behind identifying params in [1] as numbers and [2] as
>>>>>> strings?
>>>>>>
>>>>>> Thanks & Regards,
>>>>>> Ruchira
>>>>>>
>>>>>> [1] http://foo.com/calc?param1=1¶m2=2
>>>>>> [2] http://foo.com/search?param1=hello¶m2=world
>>>>>>
>>>>>> On Mon, Mar 5, 2012 at 8:29 PM, Samisa Abeysinghe <[email protected]>wrote:
>>>>>>
>>>>>>> Reason I am saying is that I wrote a cal to demo session and
>>>>>>> the param could not be added out of the box. I have to make use of '
>>>>>>> *parseFloat'*
>>>>>>>
>>>>>>> See code below.
>>>>>>>
>>>>>>> <%
>>>>>>> var operation = request.getParameter("operation");
>>>>>>> var value1 = parseFloat( request.getParameter("value1") );
>>>>>>> var value2 = parseFloat( request.getParameter("value2") );
>>>>>>>
>>>>>>> var result = 0;
>>>>>>> var memory = 0;
>>>>>>>
>>>>>>> if (operation == "add" ) {
>>>>>>> result = value1 + value2;
>>>>>>> } else if (operation == "sub" ) {
>>>>>>> result = value1 - value2;
>>>>>>> } else if (operation == "mul" ) {
>>>>>>> result = value1 * value2;
>>>>>>> } else if (operation == "div" ) {
>>>>>>> result = value1 / value2;
>>>>>>> } else if (operation == "mem" ) {
>>>>>>> memory = session.get("result");
>>>>>>> result = memory + value1;
>>>>>>> }
>>>>>>>
>>>>>>> session.put("result", result);
>>>>>>>
>>>>>>> if (operation == "mem" ) {
>>>>>>> print( memory + " + " + value1 + " = " + result );
>>>>>>> } else {
>>>>>>> print( value1 + " " + operation + " " + value2 + " = " +
>>>>>>> result );
>>>>>>> }
>>>>>>> %>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> On Mon, Mar 5, 2012 at 8:27 PM, Samisa Abeysinghe
>>>>>>> <[email protected]>wrote:
>>>>>>>
>>>>>>>> Cannot treat a number param as a number.
>>>>>>>>
>>>>>>>> Thanks,
>>>>>>>> Samisa...
>>>>>>>>
>>>>>>>> Samisa Abeysinghe
>>>>>>>> VP Engineering
>>>>>>>> WSO2 Inc.
>>>>>>>> http://wso2.com
>>>>>>>> http://wso2.org
>>>>>>>>
>>>>>>>> Thanks,
>>>>>>> Samisa...
>>>>>>>
>>>>>>> Samisa Abeysinghe
>>>>>>> VP Engineering
>>>>>>> WSO2 Inc.
>>>>>>> http://wso2.com
>>>>>>> http://wso2.org
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> _______________________________________________
>>>>>>> Carbon-dev mailing list
>>>>>>> [email protected]
>>>>>>> http://mail.wso2.org/cgi-bin/mailman/listinfo/carbon-dev
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Ruchira Wageesha
>>>>>> Software Engineer - WSO2 Inc. www.wso2.com
>>>>>>
>>>>>> Email: [email protected] Blog: [email protected]
>>>>>> Mobile: +94775493444
>>>>>>
>>>>>> Lean . Enterprise . Middleware
>>>>>>
>>>>>> _______________________________________________
>>>>>> Carbon-dev mailing list
>>>>>> [email protected]
>>>>>> http://mail.wso2.org/cgi-bin/mailman/listinfo/carbon-dev
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> *Thanks & Regards,
>>>>>
>>>>> Nuwan Bandara
>>>>> Senior Software Engineer
>>>>> WSO2 Inc. | http://wso2.com
>>>>> lean . enterprise . middleware
>>>>>
>>>>> http://nuwan.bandara.co
>>>>> *
>>>>> <http://www.nuwanbando.com/>
>>>>>
>>>>> _______________________________________________
>>>>> Carbon-dev mailing list
>>>>> [email protected]
>>>>> http://mail.wso2.org/cgi-bin/mailman/listinfo/carbon-dev
>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>> *Afkham Azeez*
>>>> Director of Architecture; WSO2, Inc.; http://wso2.com
>>>> Member; Apache Software Foundation; http://www.apache.org/
>>>> * <http://www.apache.org/>**
>>>> email: **[email protected]* <[email protected]>* cell: +94 77 3320919
>>>> blog: **http://blog.afkham.org* <http://blog.afkham.org>*
>>>> twitter:
>>>> **http://twitter.com/afkham_azeez*<http://twitter.com/afkham_azeez>
>>>> *
>>>> linked-in: **http://lk.linkedin.com/in/afkhamazeez*
>>>>
>>>> *
>>>> *
>>>> *Lean . Enterprise . Middleware*
>>>>
>>>>
>>>> _______________________________________________
>>>> Carbon-dev mailing list
>>>> [email protected]
>>>> http://mail.wso2.org/cgi-bin/mailman/listinfo/carbon-dev
>>>>
>>>>
>>>
>>>
>>> --
>>> Hiranya Jayathilaka
>>> Associate Technical Lead;
>>> WSO2 Inc.; http://wso2.org
>>> E-mail: [email protected]; Mobile: +94 77 633 3491
>>> Blog: http://techfeast-hiranya.blogspot.com
>>>
>>> _______________________________________________
>>> Carbon-dev mailing list
>>> [email protected]
>>> http://mail.wso2.org/cgi-bin/mailman/listinfo/carbon-dev
>>>
>>>
>> _______________________________________________
>> Carbon-dev mailing list
>> [email protected]
>> http://mail.wso2.org/cgi-bin/mailman/listinfo/carbon-dev
>>
>>
>
>
> --
> *Thanks & Regards,
>
> Nuwan Bandara
> Senior Software Engineer
> WSO2 Inc. | http://wso2.com
> lean . enterprise . middleware
>
> http://nuwan.bandara.co
> *
> <http://www.nuwanbando.com/>
>
> _______________________________________________
> Carbon-dev mailing list
> [email protected]
> http://mail.wso2.org/cgi-bin/mailman/listinfo/carbon-dev
>
>
--
Ruchira Wageesha
Software Engineer - WSO2 Inc. www.wso2.com
Email: [email protected] Blog: [email protected]
Mobile: +94775493444
Lean . Enterprise . Middleware
_______________________________________________
Carbon-dev mailing list
[email protected]
http://mail.wso2.org/cgi-bin/mailman/listinfo/carbon-dev