Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java
i want to add 2 numbers of int and double data types. but how it can be possible?
Posted 19-Apr-13 8:57am
Comments
richcb at 19-Apr-13 15:04pm
   
You can't, you will get an error. So, converting is your best bet. I would suggest coverting the int to a double so you do not lose accuracy of the number.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This should help you, you will need to convert one or the other.
 
http://stackoverflow.com/questions/5254357/double-and-integer-conversion[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Numbers of two different type can not be added directly. You will have to convert one of the numbers to the 'other' type and do the addition.
 
In java (and in fact most other languages), the values will be added automatically by converting the int to double, and the result will be double. If you don't want the automatic conversion to take place, you need to cast to do the conversion the way you want.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 Zoltán Zörgő 99
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 22 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100