For most simple cases, I use a prefix of a unary plus to convert
strings to numbers. It's short. It's easy to read once you're used to
doing it. I haven't tested the speed, but it may be faster as well.
>>> typeof("1");
"string"
>>> typeof(+"1");
"number"
var startmin = (+$("timestartmin").text() ) || 0;
var stopmin = (+$("timestopmin").text() ) || 0;
On Feb 22, 7:26 am, "Dan G. Switzer, II" <[EMAIL PROTECTED]>
wrote:
> Vlad,
>
> >Is there something special that needs to be done to values selected with
> >jQuery so math functions can work on them? parseInt or something? I am not
> >sure, but all my calculations are getting NaN. For example, I have the
> >following function that always produces NaN:
>
> > var startmin = $("#timestartmin");
> > var stopmin = $("#timestopmin");
>
> > var totaltime = startmin - stopmin;
> > $("#totaltime").val(totaltime); }
>
> >I've tried adding .val to the first two lines, I've tried parseInt to
> >separate temp variables, I just keep on getting NaN even though it is
> >pulling numbers from the two fields. Any ideas?
>
> What type of elements are the #timestartmin and #timestopmin? Are they input
> elements or standard display tag (such as a div?)
>
> Depending on the type of element you're trying to parse, the
> parseInt/parseFloat functions may not work for you. Here's the code snippet
> I use in my Calculation Plugin (http://plugins.jquery.com/project/calc):
>
> /*
> * jQuery.fn.parseNumber()
> *
> * returns Array - detects the DOM element and returns it's value. input
> * elements return the field value, other DOM objects
> * return their text node
> *
> * NOTE: Breaks the jQuery chain, since it returns a Number.
> *
> * Examples:
> * $("[EMAIL PROTECTED]'price']").parseNumber();
> * > This would return an array of potential number for every match in the
> selector
> *
> */
> // the parseNumber() method -- break the chain
> $.fn.parseNumber = function(){
> var aValues = [];
> this.each(
> function (){
> var
> // get a pointer to the current element
> $el = $(this),
> // determine what method to get it's value
> sMethod = ($el.is(":input") ? (defaults.useFieldPlugin ? "getValue"
> : "val") : "text"),
> // parse the string and get the first number we find
> v = $el[sMethod]().match(defaults.reNumbers, "");
> // if the value is null, we need use 0, otherwise we take the number
> // we found and remove any commas
> v = (v==null) ? 0 : v[0].replace(new RegExp(defaults.comma, "g"), "");
> aValues.push(parseFloat(v, 10));
> }
> );
>
> // return an array of values
> return aValues;
>
> }
>
> Here's the default structure (which defines the RegEx to use):
> // set the defaults
> var defaults = {
> // regular expression used to detect numbers
> reNumbers: /\d+(,\d{3})*(\.\d{1,})?/g,
> // the character that indicates the delimiter used for separating numbers
> (US 1,000 = comma, UK = 1.000 = period)
> comma: ",",
> // should the Field plug-in be used for getting values of :input elements?
> useFieldPlugin: (!!$.fn.getValue)
>
> };
>
> The RegEx might be overkill in your case, but if you're trying to parse out
> numbers from elements that may contain formatting then it's going to be
> necessary.
>
> -Dan