BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 10-23-2009, 02:32 PM   #1 (permalink)
New Member
 
Join Date: Oct 2008
Model: 8800
PIN: N/A
Carrier: rogers
Posts: 11
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Converting String to Int

Please Login to Remove!

I have a dilemma. I have a number as a String with decimal places - e.g. 32.789109 - and I need to get convert this to an int. I have tried converting to Double using parseInt and ValueOf methods but it seems that it wants to always strip the decimal places with conversion - or else throws a numberFormat Exception at runtime. It would seem so simple but it does not seem to be - this the absence of clases like DecimalPlaces and DecimalFormat

Anyone able to help? Appreciative
Offline  
Old 10-23-2009, 03:54 PM   #2 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default

"it wants to strip off the decimal places"

Are you expecting something else. If you convert this to int it will be "32", right?

If you want to round the value and convert to int, then see MathUtilities.round()
Offline  
Old 10-23-2009, 05:57 PM   #3 (permalink)
New Member
 
Join Date: Oct 2008
Model: 8800
PIN: N/A
Carrier: rogers
Posts: 11
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Actually I want to keep the decimals. i.e.
have String aaa = "13.34341123"
need int bbb = 13.34341123

I have found that I can convert String to double and then double to Integer but the process strips the decimal places and I end up with 13 nothing else
Unless I am missing something obvious here..
Offline  
Old 10-23-2009, 06:42 PM   #4 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default

You *are* missing something obvious.

Integers are "whole number values" and cannot contain decimals.

12.0101010 is "12" when converted to an int - period.

The best you can do is round the value to the nearest whole number value, using MathUtilities.round() (see my previous post).

See this definition of Integer:

http://en.wikipedia.org/wiki/Integer

Last edited by Dougsg38p : 10-23-2009 at 06:43 PM.
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Wavetek Network Analyzer Mainframe 1038-D14 Manual, Original
$25.0
Wavetek Network Analyzer Mainframe 1038-D14 Manual, Original pictureWavetek Network Analyzer Mainframe 1038-D14A Manual, Original
$25.0
Wavetek Network Analyzer Mainframe 1038-D14A Manual, Original pictureTektronix TM504 4 Slot Power Module - Modular Mainframe
$35.0
Tektronix TM504 4 Slot Power Module - Modular Mainframe pictureNATIONAL INSTRUMENTS SCXI 1001 12 SLOT RACK SCXI MAINFRAME USED
$100.0
NATIONAL INSTRUMENTS SCXI 1001 12 SLOT RACK  SCXI MAINFRAME USED pictureHP HEWLETT PACKARD E1401B HIGH POWER MAINFRAME W/ E1406A E1676B &E1668A
$59.54
HP HEWLETT PACKARD E1401B HIGH POWER MAINFRAME W/ E1406A E1676B &E1668A picture






Copyright 2004-2016 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.