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

Adobe AIR Development

Reply
Developer
Posts: 138
Registered: ‎01-09-2011
My Device: Blackberry Tablet OS
Accepted Solution

Convert String With Period To Integer

Hello I am easily able to convert the string "24233" to an integer but I am not able to convert "2313.213123" to an integer why? How do I make the parser recognize the period as a decimal?

 

Thanks,

Noah NU

From,
Noah NU
Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Convert String With Period To Integer

[ Edited ]

hey noahnu,

 

i tried doing what you have stated and it worked fine on my end. here is the code i tested with:

 

 

var txt:String = "2313.213123";

var num:int = parseInt(txt);

trace(num); // traces 2313

 

 

are you trying to conserve the decimal value as well? if so try using the number object instead:

 

 

var txt:String = "2313.213123";

var num:Number = Number(txt);

trace(num); // traces 2313.213123

 

hope that helps. good luck!

 

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Developer
Posts: 138
Registered: ‎01-09-2011
My Device: Blackberry Tablet OS

Re: Convert String With Period To Integer

I ran my program again and it works now. Not sure what was causing the error but I was using Number(). Must have been a glitch in the IDE.

 

Thanks,

Noah NU

From,
Noah NU
Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Convert String With Period To Integer

@noahnu, just to clarify: integers are numbers that have no fractional part.  If you are trying to convert a string like "123.456" to a number and you want it to end up equal to 123, then that's converting to an integer.  If you want 123.456 to come out, then it's a Number, or perhaps you could say a "floating-point value" since that's what Number objects hold.

 

Note also that math results are different with integers and floating point values, in computers (generally), unlike in real mathematics.  If a computer language (like ActionScript3) has a true "integer" type, and you have, say, 10 and 3 in integer variables, then dividing 10 by 3 will give you 3... the fractional part (or remainder) is discarded.

 

Using floating point numbers (Number class), you would get something close to 3.3333 out... I say "close" because floating point numbers cannot precisely represent many real numbers such as what 10/3 actually equals, in the "real" world (pun intended).


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!
Highlighted
Developer
Posts: 138
Registered: ‎01-09-2011
My Device: Blackberry Tablet OS

Re: Convert String With Period To Integer

Yes I understand that, it was an error in my question.

 

Thanks,

Noah NU

From,
Noah NU