quotient = 3 / 2;
remainder = 3 % 2;

// now you have them both
Answer from recursive on Stack Overflow
🌐
Reddit
reddit.com › r/learnjava › how to get decimals on java?
r/learnjava on Reddit: How to get decimals on Java?
October 27, 2018 -

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.

.

Discussions

division - How to divide down to decimals and not get zero in Java? - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
How do I get the part after the decimal point in Java? - Stack Overflow
This is incorrect. You'll get decimal as well as part of the result. Not sure why was it upvoted by readers without even trying. Peter's answer is correct. You need another .substring(1) at the end. 2016-06-09T16:04:08.367Z+00:00 ... no it will not, it will return the value of .24. More on stackoverflow.com
🌐 stackoverflow.com
When I do division in Java, I lose my decimal, how to show it? - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Ask questions, find answers and collaborate at work with Stack Overflow for Teams More on stackoverflow.com
🌐 stackoverflow.com
java - Get all decimal places in a number after division - Stack Overflow
I am currently using the BigDecimal and it is giving me more decimals but not nearly enough for what I am trying to do. I need to be able to get all the way to the 10^6 digit. This is my current co... More on stackoverflow.com
🌐 stackoverflow.com
March 2, 2013
People also ask

How do I get a decimal result from division in Java?
Make at least one operand a floating-point value before the division, for example (double) total / count. Casting after total / count is too late because integer division has already discarded the fraction.
🌐
digibeatrix.com
digibeatrix.com › home › java syntax › java division guide: integers, decimals, remainders, and bigdecimal
Java Division Guide: Integers, Decimals, Remainders, and BigDecimal ...
When should I use BigDecimal for division?
Use BigDecimal when decimal precision and a defined rounding policy matter, such as for currency. Supply a scale and RoundingMode for divisions that do not terminate exactly, and construct decimal values from strings rather than from double values.
🌐
digibeatrix.com
digibeatrix.com › home › java syntax › java division guide: integers, decimals, remainders, and bigdecimal
Java Division Guide: Integers, Decimals, Remainders, and BigDecimal ...
What does the % operator do in Java?
The % operator returns the remainder after division. For example, 7 % 2 is 1. It is useful for checks such as even-or-odd tests, but its result follows Java's signed remainder rules.
🌐
digibeatrix.com
digibeatrix.com › home › java syntax › java division guide: integers, decimals, remainders, and bigdecimal
Java Division Guide: Integers, Decimals, Remainders, and BigDecimal ...
🌐
wikiHow
wikihow.tech › computers and electronics › software › programming › java › 3 ways to divide in java with decimals - wikihow tech
3 Ways to Divide in Java with Decimals - wikiHow Tech
April 17, 2025 - If dividing two integers results ... to divide two integers and get a decimal result, you can cast either the numerator or denominator to double before the operation....
🌐
Medium
medium.com › @AlexanderObregon › javas-bigdecimal-divide-method-explained-204688fe717a
Java’s BigDecimal.divide() Method Explained | Medium
January 13, 2025 - The divide() method allows you to divide one BigDecimal value by another. Depending on the specific version of the method you use, it provides control over factors like rounding behavior and decimal places (scale).
🌐
Java-forums
java-forums.org › new-java › 57312-how-calculate-division-java.html
how to calculate division in java
March 26, 2012 - package com.corejava.sonu; import java.util.Scanner; public class Division { public static void main(String args[]){ float x,y,z; System.out.println("Enter the two integers value"); Scanner in =new Scanner(System.in); x=in.nextInt(); y=in.nextInt(); z=x/y; System.out.println("Division of two integers="+z); } } **try this simple example for division using float
Find elsewhere
🌐
LabEx
labex.io › tutorials › java-how-to-print-division-result-with-decimal-places-414110
How to print division result with decimal places | LabEx
In the example above, the division result is rounded to two decimal places using the Math.round() method. When working with division in Java, you may often want to print the result with a specific number of decimal places.
🌐
Deep Java
digibeatrix.com › home › java syntax › java division guide: integers, decimals, remainders, and bigdecimal
Java Division Guide: Integers, Decimals, Remainders, and BigDecimal - Deep Java
2 weeks ago - Both operands are int values, so Java performs integer division and discards the fractional part toward zero. Use (double) 7 / 2 or 7.0 / 2 when a fractional result is required. Make at least one operand a floating-point value before the division, for example (double) total / count. Casting after total / count is too late because integer division has already discarded the fraction.
Top answer
1 of 2
17

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
2 of 2
3

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).

🌐
Coderanch
coderanch.com › t › 390929 › java › Obtaining-decimal-portion-number
Obtaining only a decimal portion of a number. (Beginning Java forum at Coderanch)
To get the decimal part you could do this: double original = 1.432d; double decimal = original % 1d; Note that due to the way that decimals are actually thought of, this might kill any precision you were after. The decimal part of 1.432 is actually thought of as .43199999999999994 once calculations ...
🌐
Reddit
reddit.com › r/javahelp › how to find if the division is proper number including decimal?
r/javahelp on Reddit: How to find if the division is proper number including decimal?
September 1, 2023 -

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?

Top answer
1 of 5
4
You could use BigDecimal.divide() . It will throw an ArithmeticException if the result is a non-terminating decimal expansion.
2 of 5
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
🌐
Scaler
scaler.com › home › topics › java integer division
Java Integer Division - Scaler Topics
May 4, 2023 - Let us now recap the points we discussed throughout the article: In Java when two values of integer type are divided, it returns the value of integer type. As, an integer cannot contain any decimal or exponential type value, there is a loss in precision or accuracy in the result.
🌐
Baeldung
baeldung.com › home › java › java numbers › how to separate double into integer and decimal parts
How to Separate Double into Integer and Decimal Parts | Baeldung
March 17, 2024 - The BigDecimal class in Java provides its user with complete control over rounding behavior. This class also provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. Let’s use BigDecimal to get the integer and decimal parts of a floating point number: double doubleNumber = 24.04; BigDecimal bigDecimal = new BigDecimal(String.valueOf(doubleNumber)); int intValue = bigDecimal.intValue(); System.out.println("Double Number: " + bigDecimal.toPlainString()); System.out.println("Integer Part: " + intValue); System.out.println("Decimal Part: " + bigDecimal.subtract( new BigDecimal(intValue)).toPlainString());
🌐
Java2s
java2s.com › Questions_And_Answers › Java-Data-Type › Integer › divide.htm
divide « Integer « Java Data Type Q&A
Consider this code: public class ShortDivision { public static void main(String[] args) { short i = 2; · I have a situation where I need to find out how many times an int goes into a decimal, but in certain cases, I'm losing precision. Here is the method: public
Top answer
1 of 3
33

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.

2 of 3
29

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.