converting between double and integer  
Author Message
NoIdea





PostPosted: 2006-2-8 0:32:00 Top

java-programmer, converting between double and integer I have a variable declared as a double as :-

sValue = Math.floor(Math.sqrt(polynomialDegree));

where 'polynomialDegree' is an integer.

How do I convert 'sValue' to an integer?? I cant declare sValue as an
integer straight off because its returning an error message saying 'possible
loss of precision', and have tried to convert it to an integer using
intValue() but keep getting an error saying 'double cannot be dereferenced'

Where am i going wrong?

Thanks


 
Hendrik Maryns





PostPosted: 2006-2-8 1:19:00 Top

java-programmer >> converting between double and integer -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message

NoIdea schreef:
> I have a variable declared as a double as :-
>
> sValue = Math.floor(Math.sqrt(polynomialDegree));
>
> where 'polynomialDegree' is an integer.
>
> How do I convert 'sValue' to an integer?? I cant declare sValue as an
> integer straight off because its returning an error message saying 'possible
> loss of precision'

Well, what do you expect if forcing a double into an integer?

> and have tried to convert it to an integer using
> intValue() but keep getting an error saying 'double cannot be dereferenced'

You cannot do sValue.intValue(), because sValue is a double, not a
Double or an Integer.

Just declare it as int, or show us more context explaining what you want.

H.

--
Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFD6NZxe+7xMGD3itQRAh9JAJ9tvC9Dcs3R146UQviQc0dfGWEtcwCcCv2v
rFaW1hfa1nCCrZbvYNmVsic=
=kA+x
-----END PGP SIGNATURE-----
 
Oliver Wong





PostPosted: 2006-2-8 1:20:00 Top

java-programmer >> converting between double and integer
"NoIdea" <email***@***.com> wrote in message
news:ZX3Gf.51343$email***@***.com...
>I have a variable declared as a double as :-
>
> sValue = Math.floor(Math.sqrt(polynomialDegree));
>
> where 'polynomialDegree' is an integer.
>
> How do I convert 'sValue' to an integer?? I cant declare sValue as an
> integer straight off because its returning an error message saying
> 'possible
> loss of precision', and have tried to convert it to an integer using
> intValue() but keep getting an error saying 'double cannot be
> dereferenced'
>
> Where am i going wrong?

Declare sValue to be int, and then cast your expression to int, like so:

int sValue = (int) Math.floor(Math.sqrt(polynomialDegree));

- Oliver


 
 
Roedy Green





PostPosted: 2006-2-8 7:49:00 Top

java-programmer >> converting between double and integer On Tue, 07 Feb 2006 16:31:53 GMT, "NoIdea" <email***@***.com> wrote,
quoted or indirectly quoted someone who said :

>How do I convert 'sValue' to an integer?? I cant declare sValue as an
>integer straight off because its returning an error message saying 'possible
>loss of precision', and have tried to convert it to an integer using
>intValue() but keep getting an error saying 'double cannot be dereferenced'

your question could be stated, "How do I convert a double to an int"
or more generally an X to a Y.

See http://mindprod.com/applets/converter.html

It will show you the code.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.