double « Integer « Java Data Type Q&A

1. What is the difference between double a = a + int b and int a += double b?

Why does:

public class Addition { 
  public static void main() { 
    int a = 0; 
    double b = 1.0; 

2. Why doesn't Java throw an Exception when dividing by 0.0?

I have code to calculate the percentage difference between 2 numbers - (oldNum - newNum) / oldNum * 100; - where both of the numbers are doubles. I expected to have ...

3. Possible loss of precision / [type] cannot be dereferenced

I have been looking around a lot but i simply can't find a nice solution to this...

Point mouse = MouseInfo.getPointerInfo().getLocation();

int dx = (BULLET_SPEED*Math.abs(x - mouse.getX()))/

4. += int and double conversion

Possible Duplicate:
Varying behavior for possible loss of precision
int testing = 0;
testing += 2.0
the above code compiles. where as
int testing = 0;
testing = testing + 2.0;
this ...

5. Java- Identifying ints and doubles

I want to write a conditional statement, depending on whether a variable is an int or double, i.e

if (x is a double)
  do stuff
else if(x is an int)
  do ...

6. Why does 1/0 give error but 1/0.0 returns "Inf"?

I'm just curious about this: If Java calculate 1/0 it gives an exception:

Exception in thread "main" java.lang.ArithmeticException: / by zero at Foo.main(
But when ...

7. How to convert Double to int directly?

May be this is silly question. I want to get rid of the fractional part of the Double number. But I cant do that. It shows the error that incompatible types. ...

8. Java int vs. Double

public double calc(int v1) {
return v1 / 2 + 1.5;

public double cald (double v) {
return v / 2 + 1.5;
Do the functions return the same result? I would argue that they don't ...

9. Java - Converting hours(in double) to minutes(integer) and vice versa

I need the correct formula that will convert hours to minutes and vice versa. I have written a code, but it doesn't seem to work as expected. For eg: If I have hours=8.16, ...

10. Is checking a double for equality ever safe? java

I have the following code:

double x = 0;

{ stuff ...}

if(x == 0){

I was always taught that you shouldn't check floats for equality. Is checking to see if it is equal ...

11. double arithmetic and equality in Java

Here's an oddity (to me, at least). This routine prints true:

double x = 11.0;
double y = 10.0;
if (x-y == 1.0) {
    // print true
} else {

12. (double,int) cannot be applied to (double)

Been wracking my brain for hours trying to figure this out. i have the main method which is:

public static void main(String [] args)

13. The method Loan(double, int, double) is undefined for the type Loan?

Can anyone help me see where I am missing this? I am trying to get the loan class called to the main method and check to see if my exception handling ...

14. (2+4) 6 byte integer to double

AlwaysANovice There is a DLL on a Windows (Intel, 32-bit) server that contains a functions called EntConvertInt which takes a Int16 and Int32 arguments and returns a double. I want to ...

15. int, BigInt, double

If you're writing a Fraction class, then it will really help you to think this question through, because it will be fundamental to how your class works. First, define your requirements for a "numerator" and a "denominator." Will these be whole numbers only? Or might they be floating point? What range will they require? Then list the basic characteristics for each ...

16. Converting a Double to an Int

I'm not really sure whether you're talking about objects (Double and Integer) or primitive datatypes (double and int). But here's a short program which illustrates both. class DoubleTrouble{ public static void main (String args[]) { double d = 1.2345; int i = (int) d; Double d2 = new Double(d); Integer i2 = new Integer(d2.intValue()); System.out.println("i =" +i); System.out.println("d = " +d); ...

17. Changing a double to an int

18. How to read an int or double?

19. Converting double to int.

Hi! I`ve got an assignment wich causes me to find out if a number is a decimal or not, in short. So I+m thinkingof converting a double number to integer and see if the number changes. But I can`t get the conversion right, what am I doing wrong here ? class Oppg2 { public static void main(String[] args) { double number ...

20. converting double into integer

21. convert int ,double to string

/* to String g from double d */ /* 2 decimal places, locale-sensitive. */ java.text.DecimalFormat df2 = new java.text.DecimalFormat("###,##0.00"); g = df2.format(d); /* or exponential scientific format, locale-sensitive. */ java.text.DecimalFormat de = new java.text.DecimalFormat("0.0000000000E00"); g = de.format(d); /* or best for readability, no loss of precision, locale-insensitive */ g = Double.toString(d); /* or best for maintainability, no loss of precision, locale-insensitive ...

22. converting a double to an int

That's what I get for quickly throwing up an answer w/out double-checking with the documentation. My bad: there is no "trunc()" method. If you want to truncate a "double" value there are a couple of options: double dd1 = 1234.5678; int ii1 = (int)dd1; double dd2 = (int)dd1; double dd3 = (int)(dd1 * 100) / 100.0; System.out.println("DD1: " + dd1); System.out.println("II1: ...

23. Changing from Double to Int

Hi I have an array of double values and i am then using this array within a switch statement... switch(timevaluesarray(iCount) { ........... } When I compile the code I get the error 'loss of precision', where a double is found but and int is required. Is there a simple line of code that can change the values in the array from ...

24. Integer to double

private Integer antalTim; private double timLon; public Integer getAntalTim() { return antalTim; } public void setAntalTim(Integer newAntalTim) { antalTim = newAntalTim; } public double getTimLon() { return timLon; } public void setTimLon(double newTimLon) { timLon = newTimLon; } public double anstalldlon() { double tal = getAntalTim(); return (tal * (this.getTimLon())); } so I got this mess: Error(28,19): incompatible types; found: java.lang.Integer, ...

25. double to Int

26. why use int over double?

i am using the book book beginning java 2. and there is a example showing how the math class works the program calculates the radius of a circle in feet and inches given that the area is 100 square feet: public class MathCalc { public static void main(String[]args) { double radius = 0.0; double circlearea= 0.0; int feet = 0; int ...

27. converting double to int?

How do i convert a double number, which I'm sure to be a whole number (due to a rounding operator i have used) back to an integer? Is this possible? I keep getting a 'possible loss of precision error'. I understand why this may be the case in other circumstances but I know this wouldn't hapen in my case.

28. need help with double and integer code

this is the same program as earlier -prof went over it in class, since everyone was having trouble. Now he wants it to work accurately. In other words, in case 2 if an Integer is entered than display the correct message, if is not an integer entered, then display an error message. Same as with case 3 and the double. It ...

29. int and double get methods

Hi everyone, I have written the code to get Strings out of an arraylist, but I have encountered problems with int and double, clearly it is not done in the same way, below I have added an eg of the String get methods I have added, and work (phew!). Any help on how to write a similar method ot return a ...

30. Type mismatch:Cannot convert from double to int.

Hello to All, This is another newbie question. I have been working this type of error for the last couple of days and have been getting nowhere so it is time to ask you, the adoring wonders of the Java world. How do I work doubles and chars and ints and strings together? Here is one program that has not even ...

31. Method that changes and Int to a Double

Hello to All, I am working the programming book "Think Like a Computer Scientist in Java" and am on page 138 with this problem. I like the Author's manner of teaching as well as his idea that the title is a joke of sorts. (Agreed this latter could spark blog wars about the current state of "Computer Scientists coming out of ...

32. doubt in double and Integer

Hi All this is sivakumar. Actually i did one small program that same method with different parameter. this is my program class example { public void something(Integer ival) { System.out.println("Integer:" + ival); } public void something(double ival) { System.out.println("Double:" + ival); } public static void main(String arg[]) { example ex = new example(); ex.something(1); // here i passed int value to ...

33. Problem with doubles equalling ints

Hi there, I was trying to answer one of the algorithm problems on TopCoder whereby a message is displayed only if the percentage of seconds elapsed is exactly10.00000%, 11.000000% etc. You have to parse a time string and convert it to seconds and then see how many messages will be displayed in total. 0% and 100% will never be displayed. My ...

34. Incompatible operand types int and double[][]

Hello I just started java and try to do a programm wich is able to give me a fast solution of the Gaus System. (If anyone knows ;-) ) But I got a problem: import java.util.Scanner; public class Info { /** * @param args */ public static void main(String[] args) { int a; double b [][] = new double [4][4]; int ...

35. How to convert a double into a int?

36. Question about my code (double, int)

Well the basic of my program would be to insert coins (pennies,dimes, nickles, and quarters) then print out the total in $ format. The problem is when ever I tied added some number like 3 nickels to the bank (total) then print out the total (getContents), I would get result such as 0.15000000000000002 instead of just 0.15(3*0.05). Another example would be ...

37. Adding an integer to a double?

int y = 3; double z= y+4.0; Will the right hand side of the bottom line return a double, or will this be an error? I know y is an integer, but I believe I heard something like if you if you write double z=5, then you'll get 5.0 as an output, but I am not sure. Basically will the code ...

38. Convert an objects data from int to double

39. convert integer to double

Hi guys, I'm trying to get the average of two integer values in double. If I enter 7 and 8 then I get only 7 as the average not 7.5. I tried double average = Double.parseDouble( (firstNumber + secondNumber) /2 ); but I get a compile error. below code is compiled fine but I cant get the average in double. Appreciate ...

40. how can check input is double or integer?

There may be a better way of doing this but you could try something like this. Convert the input to a string if it isn't already. Then you could search through each character. If there is a decimal point then it is a double value else it is an integer. Hope this helps, Regards Serb.

41. Handling Money, int, double,...?

Using a floating point number to represent money can lead to rounding errors. You can store the amount as cents in an int. Or you can create your own Cash class that has two instance variables: dollars and cents. The advantage of writing your own class is that you can add as many helper methods as you need.

43. doesnt work with double works with int

One of the issues I see with your code is you are using Double where you should use double. There's a BIG difference between the two. Also as noted above, you are not initializing your variables. There are other problems as well, but these are major ones that stand out. double is a primitive, like int, while Double is an Object ...

45. Converting double to int

46. int * double ?

this has to do with precision. under the covers, either the multiplication needs to be done int*int or double*double, so either the int needs converted to a double or the double to an int. in first case no precision is lost, but in the second the double will lose anything after the decimal. Therefore, in order to keep all the precision, ...

47. Int and Double help

3. Compute the range of data values (by finding the maximum and the minimum and taking their difference); then compute the width of each equally-sized interval (by dividing the whole range by the number of intervals specified by the user); finally, compute the number of data numbers falling in each interval and store these frequencies in an array of integers;

49. Return double or int !

51. Strange behaviour of int and double results

Hi, I am rather new to Java and would like some help with a small applet that I have written. In the code I calculate a result and store it in a variable of type double. The value of interest varies between 0 and 1. double zeta; etc ... ... zeta = damping(I, Rn, velocity_mph); //my own function I am interested ...

52. How to convert a double to integer ?

In other threads I have provided good advice of how to implement something and had it totally ignored. Here I deliberately provided bad advice and for some reason OP has zeroed in on it instead of the cleaner more sensible explanation. So now I know what I have to do in future in order to be "heard".

53. Being able to use a double and an int in a equation

Did a little searching for the answer before asking both in the api and here on the forums, as well as my old programs but just can't seem to find the answer. I need to be able to use an int and a double in a equation where the answer should be an int. Here is an example of exactly what ...

54. Double to Integer conversion

Need your help, please. For the precision, I made the key variables type of "double". However, when I complete the calculation, I wanted to get the integer portion only by truncating below the decimal point. I tried to use (int) () command. However, compiler complains that I will loose the precision and does not compile. I wanted to loose by truncation...... ...

55. Is there any way to randomize ints or doubles?

I'm not sure if this is possible but let's say I decide to choose 5 numbers. (e.g. 1, 2, 3, 4, 5) Is there any way to change these numbers and make them random numbers? I know this might sound strange but I'm thinking about making a program: A online lottery service, where the numbers are randomized every time a lottery ...

56. how to multiply int by double

57. convert double to int ,,, 2 ways used but !!

hi ; I'm currently developing a small program for currency conversion, which will ask the amount in pounds then converts it to dollars with least number of notes and coins used. most of the program is complete but im having a slight problem with converting the amount in Cent which is a double (amountCentD) to int (amountCentI) so i get rounded ...

58. Need help displaying double as integer.

I need help with varibale pay. when executed it shows numbers with one decimal place. need to take that off. Any help is appreciated. /* File: Hrach Beglaryan January 15, 2009 */ import java.text.DecimalFormat; // This was a failure import java.util.Scanner; public class penny { public static void main(String[] args) { Scanner kb = new Scanner(; DecimalFormat formatter = new ...

59. making an int to a double

60. Double to Int

61. double to int to String

62. converting string to int and double

Hallo everyone, I know how I can convert string to int and double via parsers but I have another problem. I am reading the values from a file which brings the daten like 1233,4456 for double values I need to round it till the 2 digits after comma for integer values I need to round it and write it like 1233 ...

63. how to write a method that can return either int OR a double

You would return an object and check if it was of type Integer or Double. Don't do this. This is a bad practice. Your methods should have well-defined return types and you will write yourself into a corner doing things like this, and anyone who ever has to maintain your code will curse your name and possibly track you down and ...

64. Class which can work with int or double

65. Problem using Double and Integer

Hi I have a problem in this code: public static Integer convertToInteger(Double value) { Integer newValue=new Double(value*100).intValue(); return newValue; } If value is 4.27 the function return 426, but if I put other value like 3.27 => 327, 44,27=>4427, work fine If you see the result of new Double(value*100).intValue(), you get 426.999999994. Any Idea why I have this problem??? By the ...

66. Problem with changing double to int!

67. get integer part of a double type value.

68. g2.drawString, double int

69. Check if double is an Integer

70. I need help converting int to double

You place it after you have given your int a value (BTW this is just an example which you have to adapt to your needs). However, are you really trying to convert 1 to 1.0 or are you trying to get your input as 1.3 or 3.674? If you are trying to get input as double then Cap'ns response above is ...

71. int and double

72. double vs int

These are two questions and I hope that you already know the answer of the first - in the math sense it is the difference between integer and real numbers (subsets of these sets). As far as the second question goes - cast what the method returns to an int. int a = (int)box.getWidth(); The compiler cannot do it implicitly because ...

73. Integer or Double?

Personally I preffer to keep money values as long (number of pence/cents), even though this means a bit of mucking about to convert to String. (Most of my projects have a Money class). double values make everything messy, and accumulate rounding errors if you don't keep an eye on them. Then it's trivial to break your amount into dollars and cents. ...

74. Convert int[][] to double[][]

You can do this operations only If You have link based arrays. But in your case, when arrays based on primitive types, you cannot do this. Because, when you create int[][] array jvm creates array where each cell has fixed (4 bytes) size. And if you want to convert you array to double[][] jvm will need to create new double[][] array ...