[Javascript] type casting...

J.R. Pitts listuser at wjponline.com
Sat Jun 28 11:46:06 CDT 2003


Yeah, I guess my way is as good as any (IMHO, better than some of those
atrocities you came up with. I hope you were just making a point using
examples.)

I just thought maybe js had a specific casting method.

Thanks,

J.R.



-----Original Message-----
From: javascript-bounces at LaTech.edu
[mailto:javascript-bounces at LaTech.edu]On Behalf Of Walter Torres
Sent: Saturday, June 28, 2003 4:23 AM
To: [JavaScript List]
Subject: RE: [Javascript] type casting...


> -----Original Message-----
> From: javascript-bounces at LaTech.edu
> [mailto:javascript-bounces at LaTech.edu]On Behalf Of J.R. Pitts
> Sent: Friday, June 27, 2003 10:34 PM
> To: javascript at LaTech.edu
> Subject: [Javascript] type casting...
>
>
> I don't usually do JavaScript, but every now and then....
>
> I am doing some time math. I use the following statement to
> convert a time string (ie...15:30) to a total number of minutes (ie..930)
>
>  endMinutes=endTime.substring(0,2) * 60 + endTime.substring(3,5) * 1

Remember your Order Of Operations...

The multiplication will be done first, then the addition, if your dealing
with numbers.

JavaScript does automatic data type conversion on the fly.

So...

     endTime.substring(0,2) * 60

This gets converted to a Number because you can't multiply a character.

And this, for the same reason...

    endTime.substring(3,5) * 1

It's the '+' that gets you each time!

The PLUS symbol is either an addition operator or a concatenation method,
depending on the data types in question.

By doing a multiplication on both substrings, you get the ADDITION operator.
If you drop the TIME ONE then the PLUS becomes a contamination method.

So to (It think) answer your question, your method is as good as any.

Below I've shown a few other ways. I've not seen any (empirical) evidence to
show one is better than another.


    intEndTimeHour    = new Number ( endTime.substring(0,2) );
    intEndTimeMinutes = new Number ( endTime.substring(3,5) );

    intEndMinutes = intEndTimeHour * 60 + intEndTimeMinutes;

or...

    intEndTimeHour    = endTime.substring(0,2)* 60;
    intEndTimeMinutes = intEndTimeHour + new Number
( endTime.substring(3,5) );

    intEndMinutes = intEndTimeHour + intEndTimeMinutes;

OF course this will work as well...

   var endMinutes = new Number ( endTime.substring(0,2) )
                    * 60 +
                    new Number ( endTime.substring(3,5) );

I just think it's a bit ugly! ;)

or there is this...

// this var name lies, but it gets fixed in a minute!
var intMin  = endTime.substring(0,2);  // pull out and stores as STRING
    intMin  *= 60;                     // now it gets converted to a NUMBER
    intMin  += new Number ( endTime.substring(3,5) );
                                       // pulls out value and MAKES it a
NUMBER
                                       // which gives us an Addition
Operation

Of course the '* 60' can be placed on the first line a well. Whichever suits
your fancy

jsWalter


_______________________________________________
Javascript mailing list
Javascript at LaTech.edu
https://lists.LaTech.edu/mailman/listinfo/javascript



More information about the Javascript mailing list