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 OverflowDon'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));
Write
num=(double)4/3;
It will return 1.3333....
Then you can round off the decimals
java - Division with two decimal places not working - Stack Overflow
round up to 2 decimal places in java? - Stack Overflow
Java BigDecimal divide and multiply and round to 2 decimals - Stack Overflow
Java how to divide 2 doubles with 2 digits precision? - Stack Overflow
How do I round a double to 2 decimal places in Java?
How do I fix "ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result"?
What is the difference between setScale and round(MathContext) on a BigDecimal?
That's because you are multiplying by 100 to show a percentage. You should divide with a scale of 4 instead of 2, then set the scale to 2:
new BigDecimal(193251715).divide(new BigDecimal(659669075), 4, RoundingMode.HALF_UP)
.multiply(new BigDecimal(100))
.setScale(2)
set scale of 4, to include more decimal spaces.
Set scale of 2 lastly, to make it 29.30 instead of 29.30000.
public static void main(String[] args) throws ParseException {
BigDecimal bg = new BigDecimal(193251715)
.divide(new BigDecimal(659669075), 4, RoundingMode.HALF_UP)
.multiply(new BigDecimal(100)).setScale(2);
System.out.println(bg);
}
Well this one works...
double roundOff = Math.round(a * 100.0) / 100.0;
Output is
123.14
Or as @Rufein said
double roundOff = (double) Math.round(a * 100) / 100;
this will do it for you as well.
double d = 2.34568;
DecimalFormat f = new DecimalFormat("##.00");
System.out.println(f.format(d));
Use DecimalFormat API
double d = amount / 100 * 7;
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(d));
"##" denotes the 2 digit precision
You can do this
double d = Math.round(amount * 7) / 100.0;
This will give you have value which has two decimal places. (for a modest range of values i.e. < 70e12)
If you just want to print two decimal places you can use
System.out.printf("%.2f%n", d);
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.
.