Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By PCMC
  1. Ninjatogo's Avatar
    CrackBerry Abuser

    Posts
    142 Posts
    Global Posts
    177 Global Posts
    Thread AuthorThread Author   #1  

    Question Simple QML Math Problem

    Hi all,
    I'm practicing building apps for BB10 using the Cascades framework, recently I ran into a problem when trying to do some simple addition and subtraction in my QML project. I've got a set of input boxes (prc0-9) that are blank by default (with hint text, but I can't imagine that would affect the results here). From my observations everything works perfectly if there is a number in all the input boxes, but if there is a single blank I get, "nan", in the output box (prof.text). I'd be grateful if someone could help me out here.

    Code:
    onClicked: {
    			var int0 = parseInt(prc0.text);
    			var int1 = parseInt(prc1.text);
    			var int2 = parseInt(prc2.text);
    			var int3 = parseInt(prc3.text);
    			var int4 = parseInt(prc4.text);
    			var int5 = parseInt(prc5.text);
    			var int6 = parseInt(prc6.text);
    			var int7 = parseInt(prc7.text);
    			var int8 = parseInt(prc8.text);
    			var int9 = parseInt(prc9.text);
    			var int10 = int0+int1+int2+int3
    					+int4+int5+int6+int7
    					+int8+int9;
    			var intbudg = parseInt(budg.text);
    						if ( int10 > intbudg )
    							{req.text = int10 - intbudg}
    						else
    							{req.text = "0"
    							prof.text = intbudg - int10}
    }
  2. PCMC's Avatar
    CrackBerry User

    Posts
    18 Posts
    #2  

    Default

    I believe 'nan' is short for 'not a number'. I would bet when you try to add a blank instead of an integer the result is 'nan'. Maybe you can default the value in the boxes to 0?

    *disclaimer* I know nothing of QML.
    Ninjatogo likes this.
  3. mikeo007's Avatar
    CrackBerry Genius

    Posts
    4,142 Posts
    Global Posts
    4,529 Global Posts
    #3  

    Default

    You need to add some logic to check if there is a number in each of the boxes.
    If there isn't a number, then you can do something like set it to 0 or prompt the user to fill in all boxes before tapping the button.

    NaN stands for "Not a Number"
  4. Ninjatogo's Avatar
    CrackBerry Abuser

    Posts
    142 Posts
    Global Posts
    177 Global Posts
    Thread AuthorThread Author   #4  

    Default

    I see; I don't want to fill the boxes with 0 and lose my fancy hint text so I'll trying making a quick check for blanks.
  5. mikeo007's Avatar
    CrackBerry Genius

    Posts
    4,142 Posts
    Global Posts
    4,529 Global Posts
    #5  

    Default

    Quote Originally Posted by ninjatogo View Post
    I see; I don't want to fill the boxes with 0 and lose my fancy hint text so I'll trying making a quick check for blanks.
    You'll want to check for anything that's not a number, in case the user accidentally puts letters in the boxes.
    Thanked by:
    Ninjatogo (02-07-2013) 
  6. Ninjatogo's Avatar
    CrackBerry Abuser

    Posts
    142 Posts
    Global Posts
    177 Global Posts
    Thread AuthorThread Author   #6  

    Default

    I was simply going to check if it's blank and change the corresponding variable to 0. I don't know how to check the string for letters, but I'll do some searching.
  7. Ninjatogo's Avatar
    CrackBerry Abuser

    Posts
    142 Posts
    Global Posts
    177 Global Posts
    Thread AuthorThread Author   #7  

    Default

    I finally finished it. I decided to restrict the input mode to numbers and punctuation, so they can't type letters and put in an if statement that changes the field text to 0 if the field is blank on click. Thanks for the help!
  8. SCrid2000's Avatar

    Posts
    10,377 Posts
    Global Posts
    10,389 Global Posts
    PIN
    PM me.
    #8  

    Default

    Looks kinda like java script, in which case you could do (if element != 'NaN')...

    Posted using CrackBerry App on BB10
    Thanked by:
    Ninjatogo (02-09-2013) 
  9. mikeo007's Avatar
    CrackBerry Genius

    Posts
    4,142 Posts
    Global Posts
    4,529 Global Posts
    #9  

    Default

    Quote Originally Posted by SCrid2000 View Post
    Looks kinda like java script, in which case you could do (if element != 'NaN')...

    Posted using CrackBerry App on BB10
    Or if you're using C++, include cmath and use: isnan(yournumber)
    It will return true if it's not a number.
    Thanked by:
    Ninjatogo (02-09-2013) 

Similar Threads

  1. BB 9800 Torch and Simple Mobile compatibility problem
    By guru9800BB in forum Simple Mobile
    Replies: 3
    Last Post: 09-13-2012, 09:34 AM
  2. Need some help with this little math problem...about bb traffic
    By editionfws in forum Rehab & Off-Topic Lounge
    Replies: 14
    Last Post: 08-06-2012, 11:06 AM
  3. Is a math problem or patternlock alarm available?
    By Ben34 in forum Storm 3rd Party Apps
    Replies: 11
    Last Post: 07-25-2009, 09:45 PM
  4. Simple Problem-Can't Figure It out
    By davidmogentale in forum BlackBerry Curve 83xx
    Replies: 3
    Last Post: 08-01-2008, 09:43 AM
  5. Simple Book Player Problem
    By Brother_Jebadiah in forum BlackBerry OS Apps
    Replies: 0
    Last Post: 04-09-2008, 01:14 PM

Posting Permissions