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
New Developer
Posts: 12
Registered: ‎04-12-2012
My Device: Bold 9700
My Carrier: Truemove
Accepted Solution

How to set decimal digits

Hi all,

 

In cascade QML if I calculate A/B and got the result 3.45656566

How I round the number to 3.46 

I can't find any round or rnd function.

 

Thanks for your advice.

 

Developer
Posts: 271
Registered: ‎05-29-2013
My Device: BlackBerry Z10
My Carrier: MTS

Re: How to set decimal digits

Use toFixed() method: num.toFixed (2)
-------------------------------------------------------------------------------
NSR Reader for BlackBerry 10 @NSRReader on Twitter
Me @xelfium on Twitter
Developer
Posts: 828
Registered: ‎10-16-2012
My Device: Red Z10
My Carrier: Rogers

Re: How to set decimal digits

also in qml if you want to store a decimal number create a real property

 

property real myDecimal
Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: How to set decimal digits

If you truly want to just round the value, but keep it as a number for later calculations, then you want JavaScript's Math.round(). If your purpose in rounding is for display purposes, then toFixed() or one of the related routines is better (as they return strings).

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!
Developer
Posts: 828
Registered: ‎10-16-2012
My Device: Red Z10
My Carrier: Rogers

Re: How to set decimal digits

[ Edited ]

you can also call parseFloat() on a string to pull a decimal from it

 

property real myDecimal

myDecimal = parseFloat(myString)

 

 

New Developer
Posts: 12
Registered: ‎04-12-2012
My Device: Bold 9700
My Carrier: Truemove

Re: How to set decimal digits

Hi Can you show me more example for this.. I already use but can't set the decimal.

Thanks

 parseFloat(myString)
New Developer
Posts: 12
Registered: ‎04-12-2012
My Device: Bold 9700
My Carrier: Truemove

Re: How to set decimal digits

Now I wrote     target.text = Math.round(9.555555),2

the result is 10 .. but I need 9.56 how to correct it.

 

Thanks

Developer
Posts: 828
Registered: ‎10-16-2012
My Device: Red Z10
My Carrier: Rogers

Re: How to set decimal digits

you have to multiply it and then divide it to get a decimal, if you want 3 places go to the 1000's and so on

 

Math.round(num * 100) / 100

 

 

New Developer
Posts: 12
Registered: ‎04-12-2012
My Device: Bold 9700
My Carrier: Truemove

Re: How to set decimal digits

Thank you very much I got it.