Don't do int division since this always will result in a truncated int. Instead have your division use at least one double value.

double num = 4.0/3.0;

Then when you want to display it as a String, format the output so you can choose your decimal places:

// one way to do it
// %.3f is for a floating number with 3 digits to the right of the decimal
// %n is for new line
System.out.printf("%.3f%n", num); 

Another:
DecimalFormat decimalFormat = new DecimalFormat("0.000");
System.out.println(decimalFormat.format(num));
Answer from Hovercraft Full Of Eels on Stack Overflow
๐ŸŒ
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 %.2f conversion specifier is used to print the result with two decimal places. Alternatively, you can use the Math.round() function to round the division result to a specific number of decimal places.
Discussions

java - Division with two decimal places not working - Stack Overflow
I'm trying to divide 193251715/659669075 and get the result of 29.30% (so to round it up to the nearest value with two decimal points) Here's what I've done: new BigDecimal(193251715).divide(new More on stackoverflow.com
๐ŸŒ stackoverflow.com
round up to 2 decimal places in java? - Stack Overflow
Using String.format, I can format ... taxes' decimal places. 2016-02-16T13:47:19.17Z+00:00 ... BigDecimal a = new BigDecimal("123.13698"); BigDecimal roundOff = a.setScale(2, BigDecimal.ROUND_HALF_EVEN); System.out.println(roundOff); ... BigDecimal.ROUND* is deprecated since Java ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Java BigDecimal divide and multiply and round to 2 decimals - Stack Overflow
I want to concat this to 2 decimal places 66.67% ... I think BigDecimal is a type of floating point. You can control the total number of digits of precision, not the places after the decimal. So there's that little issue to start with. ... So I think normally you end up rounding the output when printed, not the actual number itself. I just happen to have this handy in my reputation changes: stackoverflow.com/questions/54121319/โ€ฆ ... FYI: Since Java ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Java how to divide 2 doubles with 2 digits precision? - Stack Overflow
amount / 100 * 7 - I'am trying to get a percent from an amount, but the problem is that sometimes a get a number with to many digits after dot, how can I make it strictly return 2 digits after dot?... More on stackoverflow.com
๐ŸŒ stackoverflow.com
May 22, 2017
People also ask

How do I round a double to 2 decimal places in Java?
Use BigDecimal.valueOf(x).setScale(2, RoundingMode.HALF_UP), which turns 1.005 into 1.01 and pads 7 to 7.00. Call .doubleValue() on the result if you need a double back. The one-liner Math.round(x * 100.0) / 100.0 is fine for most values, but because 1.005 * 100.0 is 100.49999999999999, it returns 1.0 for 1.005.
๐ŸŒ
compilebytes.com
compilebytes.com โ€บ home โ€บ examples โ€บ java examples โ€บ how to round to 2 decimal places in java
How to Round to 2 Decimal Places in Java | CompileBytes
How do I fix "ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result"?
Give BigDecimal.divide a scale and a rounding mode. new BigDecimal("10.00").divide(new BigDecimal("3")) throws because 10 / 3 has no exact decimal form, while divide(divisor, 2, RoundingMode.HALF_UP) returns 3.33. To keep a number of significant digits instead of decimal places, pass a MathContext such as MathContext.DECIMAL64, which gives 3.333333333333333.
๐ŸŒ
compilebytes.com
compilebytes.com โ€บ home โ€บ examples โ€บ java examples โ€บ how to round to 2 decimal places in java
How to Round to 2 Decimal Places in Java | CompileBytes
What is the difference between setScale and round(MathContext) on a BigDecimal?
setScale counts decimal places and round(MathContext) counts significant digits. For 1234.5678, setScale(2, RoundingMode.HALF_UP) gives 1234.57, round(new MathContext(6)) gives the same 1234.57, but round(new MathContext(2)) gives 1.2E+3. To round to 2 decimal places, use setScale.
๐ŸŒ
compilebytes.com
compilebytes.com โ€บ home โ€บ examples โ€บ java examples โ€บ how to round to 2 decimal places in java
How to Round to 2 Decimal Places in Java | CompileBytes
๐ŸŒ
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 - Here is a simple example of dividing two decimal numbers with double division: double x = 10.5; double y = 2.5; x / y // the answer is 4.2
Find elsewhere
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2024 โ€บ 05 โ€บ how-to-round-a-number-to-two-decimal-places-in-java
How to round a number to two decimal places in Java
May 31, 2024 - We already covered this method in detail at: Java Math.round() method. Since, we are dealing with two decimal places, you can simply multiply the number by 100, round it to the nearest integer, and then divide by 100.
๐ŸŒ
CompileBytes
compilebytes.com โ€บ home โ€บ examples โ€บ java examples โ€บ how to round to 2 decimal places in java
How to Round to 2 Decimal Places in Java | CompileBytes
%.2f rounds and pads to exactly two decimals and returns a String. Always pass a Locale as the first argument: without one, Java uses the machine's default locale, and in much of Europe that prints a comma as the decimal separator.
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ java basics โ€บ round a double to 2 decimal places in java (or n places)
Round a Double to 2 Decimal Places in Java (or N Places)
February 16, 2024 - To round a double to 2 decimal places, or to any number of places, BigDecimal.valueOf(value).setScale(n, mode) is the reliable method, because it rounds the decimal value we wrote and lets us choose the rounding mode.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java numbers โ€บ truncate a double to two decimal places in java
Truncate a Double to Two Decimal Places in Java | Baeldung
September 2, 2026 - The String.format() method takes two arguments. Firstly, the format we want to apply, and secondly, the arguments referenced by the format. To truncate to two decimal places, weโ€™ll use the format String โ€œ%.2fโ€:
๐ŸŒ
Study.com
study.com โ€บ business courses โ€บ business 104: information systems and computer applications
How to Round to 2 Decimal Places in Java - Lesson | Study.com
January 5, 2018 - In summary, when we have long, complicated numbers (lots of numbers to the right of the decimal point), it's helpful to round. This helps the end user conceptualize the number. We can use the Math.round function in Java to round to two decimal ...
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java numbers โ€บ how to round a number to n decimal places in java
How to Round a Number to N Decimal Places in Java | Baeldung
2 weeks ago - We can control n number of decimal places by multiplying and dividing by 10^n: public static double roundAvoid(double value, int places) { double scale = Math.pow(10, places); return Math.round(value * scale) / scale; } However, itโ€™s important ...
๐ŸŒ
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.

.

๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ java โ€บ round a number to n decimal places in java
Java: How to Round a Number to N Decimal Places | Sentry
September 11, 2026 - You can use the Java DecimalFormat class to define a format pattern that rounds numbers to a specific number of decimal places for display. The following example creates a DecimalFormat object with the format pattern #.## (where # represents digits) to round the number to 2 decimal places:
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ java โ€“ display double in 2 decimal places
Java โ€“ Display double in 2 decimal places | mkyong.com
October 29, 2021 - It works. System.out.printf("double : %.2f", input); Reply Helpful Report Spam or advertising Abusive or offensive Off topic ยท Java Compact Source File: No Class, No Static Main