quotient = 3 / 2;
remainder = 3 % 2;
// now you have them both
Answer from recursive on Stack Overflowquotient = 3 / 2;
remainder = 3 % 2;
// now you have them both
In your example, Java is performing integer arithmetic, rounding off the result of the division.
Based on your question, you would like to perform floating-point arithmetic. To do so, at least one of your terms must be specified as (or converted to) floating-point:
Specifying floating point:
3.0/2
3.0/2.0
3/2.0
Converting to floating point:
int a = 2;
int b = 3;
float q = ((float)a)/b;
or
double q = ((double)a)/b;
(See Java Traps: double and Java Floating-Point Number Intricacies for discussions on float and double)
Hi, I started the Helsinki Java MOOC course today and was wondering how to get decimals as a result when dividing? The questions asks: Create a program that asks the user for two integers and prints their quotient. Make sure that 3/2= 1.5
This is what I have:
System.out.println ("Division: 'X / Y = Z' for some integers X, Y and Z");
int X = reader.nextInt();
System.out.println ("Division: 'X / Y = Z' for some integers X, Y and Z");
int Y = reader.nextInt();
int Quotient = X / Y;
String toPrint = X + " / " + Y + " = " + Quotient;
System.out.println(toPrint);
I tried putting some stuff in that empty space to yield some decimals but nothing would work. For example, whenever I run it and input 3/2 I get 1 rather than 1.5
Also, this is my very first day ever trying this stuff and I have 0 background so forgive the lack of proper terms.
.
division - How to divide down to decimals and not get zero in Java? - Stack Overflow
How do I get the part after the decimal point in Java? - Stack Overflow
When I do division in Java, I lose my decimal, how to show it? - Stack Overflow
java - Get all decimal places in a number after division - Stack Overflow
How do I get a decimal result from division in Java?
When should I use BigDecimal for division?
What does the % operator do in Java?
Either cast the ints to double, or just use doubles.
For example:
double[] arrayName = new double[10];
resultValue[0] = (14.0/49.0)*100;
or:
double[] arrayName = new double[10];
resultValue[0] = ((double)14/49)*100;
How you see it:
(double/double)*double
How the JVM sees it
(int/int)*int
From the the JLS. int to double is a widening conversion. From §5.1.2:
Widening primitive conversions do not lose information about the overall magnitude of a numeric value.
[...]
Conversion of an int or a long value to float, or of a long value to double, may result in loss of precision-that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode (§4.2.4).
If you replace one of those with a double literal (like adding .0 or cast one of them to double and you should get the result you expect.
Try this:
String numberD = String.valueOf(d);
numberD = numberD.substring(numberD.indexOf("."));
Now this numberD variable will have value of 15
Try Math.floor();
double d = 4.24;
System.out.println( d - Math.floor( d ));
To prevent rounding errors you could convert them to BigDecimal
double d = 4.24;
BigDecimal bd = new BigDecimal( d - Math.floor( d ));
bd = bd.setScale(4,RoundingMode.HALF_DOWN);
System.out.println( bd.toString() );
Prints 0.2400
Note that the 4 in setScale is the number of digits after the decimal separator ('.')
To have the remainder as an integer value you could modify this to
BigDecimal bd = new BigDecimal(( d - Math.floor( d )) * 100 );
bd = bd.setScale(4,RoundingMode.HALF_DOWN);
System.out.println( bd.intValue() );
Prints 24
Do this:
double money = 1.0 / 8;
The problem is that you are taking the integer, 1 and dividing it by an integer, 8. Java then uses integer math, which truncates to 0. If you use 1.0 explicitly, then it will use floating point division.
You need to cast to double. In integer division the fractional part of the result is thrown away.
double money = (double)1/8;
DecimalFormat decFor = new DecimalFormat("0.00");
System.out.println(decFor.format(money));
You are loosing the precision when evaluating this:
103993/33102.0
as a double division. Actually, the following:
BigDecimal num = new BigDecimal(103993/33102.0);
is equivlent to:
double d = 103993/33102.0;
BigDecimal num = new BigDecimal(d);
instead, use:
int scale = 100;
BigDecimal num1 = new BigDecimal(103993);
BigDecimal num2 = new BigDecimal(33102);
System.out.println(num1.divide(num2, scale, RoundingMode.HALF_UP).toString());
OUTPUT:
3.1415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737
The problem is how you are making your number. The 103993/33102.0 is evaluated as a double precision floating point expression (double) before the BigDecimal class ever gets involved. You should make separate BigDecimal objects and use BigDecimal.divide to get the number you want.
Since you want exact results, I'd probably pass both of your numbers in as integers when starting.
Even after doing that, I doubt you'll be able to get out to the 10^6 digit. You might need to implement your own division algorithm to get out to that level as any sane implementation of arbitrary precision math is going to stop long before that point (at least by default).
I mean like if we do 1/4 we get 0.25 a proper decimal value. But if we divide 1/3 we get 0.3333 (it goes on). how to identify this?
Well, you can use:
double x = d - Math.floor(d);
Note that due to the way that binary floating point works, that won't give you exactly 0.321562, as the original value isn't exactly 4.321562. If you're really interested in exact digits, you should use BigDecimal instead.
Another way to get the fraction without using Math is to cast to a long.
double x = d - (long) d;
When you print a double the toString will perform a small amount of rounding so you don't see any rounding error. However, when you remove the integer part, the rounding is no longer enough and the rounding error becomes obvious.
The way around this is to do the rounding yourself or use BigDecimal which allows you to control the rounding.
double d = 4.321562;
System.out.println("Double value from toString " + d);
System.out.println("Exact representation " + new BigDecimal(d));
double x = d - (long) d;
System.out.println("Fraction from toString " + x);
System.out.println("Exact value of fraction " + new BigDecimal(x));
System.out.printf("Rounded to 6 places %.6f%n", x);
double x2 = Math.round(x * 1e9) / 1e9;
System.out.println("After rounding to 9 places toString " + x2);
System.out.println("After rounding to 9 places, exact value " + new BigDecimal(x2));
prints
Double value from toString 4.321562
Exact representation 4.321562000000000125510268844664096832275390625
Fraction from toString 0.3215620000000001
Exact value of fraction 0.321562000000000125510268844664096832275390625
Rounded to 6 places 0.321562
After rounding to 9 places toString 0.321562
After rounding to 9 places, exact value 0.32156200000000001448796638214844278991222381591796875
NOTE: double has limited precision and you can see representation issue creep in if you don't use appropriate rounding. This can happen in any calculation you use with double esp numbers which are not an exact sum of powers of 2.