Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Highlighted
New Developer
Posts: 41
Registered: ‎02-11-2010
My Device: Bold 9000
My Carrier: AT&T
Accepted Solution

weird math

I'm working on a simple grade calculator app, but I can't get the math to work. if I input 100 for inPossible and 50 for inTotal, I get 2 for finalGrade... am I missing something here?

 

import bb.cascades 1.0

//-- create one page with a label and text

Page {
    Container {
        layout: StackLayout {
            leftPadding: 80
            rightPadding: leftPadding
        }
        layoutProperties: DockLayoutProperties {
            horizontalAlignment: HorizontalAlignment.Center
            verticalAlignment: VerticalAlignment.Center
        }

        // A one line TextField that captures total possible points
        TextField {
            id: inPossible
            inputMode: TextFieldInputMode.NumbersAndPunctuation
            hintText: "How many possible?"
            bottomMargin: 50
            textStyle {
                base: SystemDefaults.TextStyles.BodyText
            }
            layoutProperties: StackLayoutProperties {
                horizontalAlignment: HorizontalAlignment.Fill
            }
            
            // On text change the label text is updated.
            // onTextChanging: {
            //    finalGrade.text = text;
            // }
        }
        
        // A one line TextField that captures number of correct items
        TextField {
            id: inTotal
            inputMode: TextFieldInputMode.NumbersAndPunctuation
            hintText: "How many correct?"
            bottomMargin: 50
            textStyle {
                base: SystemDefaults.TextStyles.BodyText
            }
            layoutProperties: StackLayoutProperties {
                horizontalAlignment: HorizontalAlignment.Fill
            }
        
            // On text change the label text is updated.
            //onTextChanging: {
            //    finalGrade.text = text;
            //}
        }

        // Calculate Percentage
        Label {
            id: finalGrade
            text: (inPossible.text / inTotal.text);
            textStyle.alignment: TextAlignment.Center
            textStyle.justification: TextJustification.Default
            textStyle.fontWeight: FontWeight.Bold
            textStyle.size: 60.0
            layoutProperties: StackLayoutProperties {
                horizontalAlignment: HorizontalAlignment.Center
                verticalAlignment: VerticalAlignment.Center
            }
        }
    }
}

 

Developer
Posts: 849
Registered: ‎03-03-2011
My Device: Z10

Re: weird math

Try using property alias.

More at http://doc.qt.nokia.com/4.7/propertybinding.html
--------------------------------------------------------------------------------------
Leader of Ljubljana BlackBerry Developer Group
BlackBerry Certified Builder for Native Application Development
Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: weird math

I don't see where property aliases (soaman's answer) come into this, but maybe I've skimmed the question/code too quickly. It appears to me, however, that you're saying 100 / 50 is giving you 2... but that's not what you wanted?

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
New Developer
Posts: 41
Registered: ‎02-11-2010
My Device: Bold 9000
My Carrier: AT&T

Re: weird math

Turns out I had the values flipped.. works now...