This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 5 years ago.
public static void main(String[] args) {//begin main
Scanner worksheet = new Scanner(System.in);
{
double radi;
double area;
double circum;
double pie = 3.14;
System.out.println("Enter the radius: ");
radi = worksheet.nextDouble();//enter 1.2
area = pie * radi * radi;
Area comes out as 4.521599999999999, but I want it to be 4.52.
System.out.println("The area is:" + area);
circum=2*pie*radi;
circum comes out as 7.536.
want it to be 7.53
System.out.println("The circumference is:"+circum);
}
How can I truncate the value to 2 decimal places without a method.
You can try
DecimalFormat df = new DecimalFormat("##.##");
df.setRoundingMode(RoundingMode.DOWN);
System.out.println("The circumference is:"+df.format(circum));
or you can simply do this
int temp = (int)(circum*100);//452
circum = temp/100d;//4.52
Both of these will limit the value up to 2 decimal points.
Wish it will solve the problem.
Reference (Take a look at this)
Related
This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 3 years ago.
package com.kevinnielsen.opgaver.betingelser;
import java.util.*;
public class opgave2 {
public static void main(String[] args) {
Scanner usdInput = new Scanner(System.in);
int eur;
int usd;
int currency;
currency = 90;
System.out.println("Write your amount in usd: ");
usd = usdInput.nextInt();
eur = (usd / 90) * 100;
System.out.println(eur);
}
}
Let's say I input 100, it should output (100 / 90) * 100 which is 111.11 but instead it outputs 100.
change your data types from int to double
This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 5 years ago.
I am trying to code a basic command line BMI Calculator in Java and for some reason every time I run the code and enter my height and weight, 0 is outputted. Please help me understand where I have made a mistake.
import java.util.Scanner;
public class Chap2 {
public static void main(String[] args){
Scanner reader = new Scanner(System.in);
System.out.println("Enter your height in inches: ");
int myHeight = reader.nextInt();
System.out.println("Enter your weight in lbs: ");
int myWeight = reader.nextInt();
int Bmi = (myWeight/myHeight/myHeight)*703;
System.out.println("Your BMI is " + Bmi + ".");
}
}
And my output is as follows:
Enter your height in inches:
68
Enter your weight in lbs:
180
Your BMI is 0.
You are dividing int with int, thus the result is a rounded int.
Cast to double before calculation to get exact results.
double bmi = ((double) myWeight / myHeight / myHeight) * 703;
This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Closed 7 years ago.
/**
* Write a description of class GUI here.
*
* #author (your name)
* #version (a version number or a date)
*/
import java.util.*;
public class GUI
{
// instance variables - replace the example below with your own
public static void main(String [] args){
Scanner r = new Scanner(System.in);
int x;
int y;
int z;
System.out.println("x");
x = r.nextInt();
System.out.println("y");
y = r.nextInt();
System.out.println("z");
z = r.nextInt();
double t = (x+y+z)/3;
System.out.println("result " + t);
}
}
Hello, above is my code.
I purposely made it int x,y,z to test the program.
When I input for example (when running the program) :$x = 1, 1, 3$ it rounds the answer always! Why is this?
This is an example of Java's integer division, which must always return another integer. It truncates any decimal result. This occurs even though the result is assigned to a double.
Use a double literal when dividing to force floating-point division.
double t = (x+y+z)/3.0;
This question already has answers here:
Why double width = 50/110000; the output is 0.000000000000000?
(3 answers)
Closed 9 years ago.
We've been set a task to calculate user input. My code compiles, however when I test it my output for the refund is always 0 :(
Users are meant to enter their distance and self contribution when prompted, but what exactly is wrong with my formula for refund? Can anybody shed some light on this for me?
import java.util.*;
public final class UserCalc {`
public static void main(String[] args) {
Scanner scanner = new Scanner(System. in );`
System.out.print("Please enter the distance ");
int distance = scanner.nextInt();
System.out.print("Please enter the percentage of self contribution ");
int selfcon = scanner.nextInt();
int trainprice = (75 + (2 / 10)) * distance;
int carprice = (26 + (7 / 10)) * distance;
int refund = (Math.min(trainprice, carprice)) * ((100 - selfcon) / 100);
System.out.print("You get a refund of " + refund + " pounds");
int age = scanner.nextInt();
}
}
Because result of ((100 - selfcon)/100) is always zero and you works with integers you should use float or double.
You are using integer arithmetic rather than floating point arithmetic, so expressions like 2/10 evaluate to 0.
double distance = scanner.nextDouble();
System.out.print("Please enter the percentage of self contribution ");
double selfcon = scanner.nextDouble();
double trainprice = (75 + (2 / 10)) * distance;
double carprice = (26 + (7 / 10)) * distance;
doulbe refund = (Math.min(trainprice, carprice)) * ((100 - selfcon) / 100);
System.out.print("You get a refund of " + refund + " pounds");
int age = scanner.nextInt();
I am very new. apologies in advance for my coding. I need to print a table that shows year and then a tab over, and then the value with a next line. The value has to be in decimal form.
I have been reading and searching and mixing my code around. I have found it for 1 variable but not for two in same line. I have tried the printf, I have tried the good ol 100 / 100.0 and I either get errors or the decimal never goes to 2 places. I do not need it rounded, just with 2 spaces after. I am obviously going wrong somewhere. I would appreciate any assistance.
import java.util.Scanner;
public class Investment1 {
public static double futureInvestmentValue(double investmentAmount, double monthlyInterestRate, int years){
double principal = 0.0;
double futureInvestmentValue = 0;
for (years = 1; years <=30; years++){
//calculate futureInvestmentValue
futureInvestmentValue = (investmentAmount * (Math.pow (1 + monthlyInterestRate, years * 12)));
System.out.print(years + "\t" + futureInvestmentValue + "\n");
}//end for
return futureInvestmentValue;
}//end futureInvestmentValue
public static void main(String[] args){
Scanner input = new Scanner (System.in);
//obtain Investment amount
System.out.print("Enter Investment amount: ");
double investmentAmount = input.nextDouble();
//obtain monthly interest rate in percentage
System.out.print("Enter annual interest rate in percentage: ");
double annualInterestRate = input.nextDouble();
double monthlyInterestRate = (annualInterestRate / 1200);
int years = 30;
System.out.println("Years\t" + "Future Value");
System.out.print(years + "\t");
System.out.print(years + "\t" + ((int)(futureInvestmentValue(investmentAmount, monthlyInterestRate, years))) + "\n");
}//end main
}//end Investment
You can use system.out.format():
System.out.format("%d \t %.2f", years, futureInvestmentValue);
you should read about format strings, heres a simple usage example:
System.out.println(String.format("%d %.2f",myint,myfloat));
myint will be printed as an integer (even if it's a floating point value) due to the use of the %d in the format string.
myfloat will be printed as a decimal number with 2 digits after the decimal point, thanks to the %f.2 part in the format string.