Rounding to one decimal place? [duplicate] - java

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 5 years ago.
I have the following variables initialized to the user inputs,
int completedPasses = reader1.nextInt();
int attemptedPasses = reader2.nextInt();
double completionRatio = (250 / 3) * ((completedPasses / attemptedPasses) - 0.3);
How do I round the computation above to a double of one decimal place?

Use DecimalFormat:
DecimalFormat df = new DecimalFormat("#.#");
df.format(completionRatio);
Or Math.round:
Math.round(completionRatio);

Related

how to set 3 range zero in double in android java [duplicate]

This question already has answers here:
How do I print a double value without scientific notation using Java?
(18 answers)
Closed 4 years ago.
When I enter the number 0.0007575 on the double, the result will be "7.575E-4", but I want result 0.0007575
thanks
Use a custom DecimalFormat to format the double value:
DecimalFormat fmt = new DecimalFormat("#0.000"); // leading zero and 3 fraction digits
String result = fmt.format(11.123456789);
System.out.println(result); // 11.123

Java - My program is rounding down when it should be rounding up [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
What is a debugger and how can it help me diagnose problems?
(2 answers)
Closed 4 years ago.
I am building a program that calculates the total cost of a restaurant bill. My program is rounding $18.135 down to $18.13. This is the code that I am using to round it to two decimal places:
tip = Math.round((charge * TIP_PERCENTAGE) * 100) / 100d;
All of the variables are doubles, and charge is equal to 100.75 and TIP_PERCENTAGE is equal to 0.18.
I tried a couple of things but I have not had any luck. I want the program to round $18.135 up to $18.14.
Try this
private static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(Double.toString(value));
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
}
...
tip =(charge * TIP_PERCENTAGE) ;
tip = round(tip,2);

How to round up in java? [duplicate]

This question already has answers here:
Always Round UP a Double
(8 answers)
Closed 5 years ago.
im trying to display a float number in 5 digits rounding up in the last digit, for example : 0.123455 will be displayed as 0.12346, and 0.123454 will be 0.12345.
My current code (not properly working) is as follows:
int iFactorSum = 100000;
int iRoundSum = (int) (fSumatoria * iFactorSum + 0.5);
fSumatoria = (float) iRoundSum / iFactorSum;
fSumatoria storages the number i want to convert.
Thanks
To round up, you can use Math.ceil(numberGrade).
To round to the nearest integer, use Math.round(numberGrade).
See: the Math class
From here.
Use Math.round() to round up floating point numbers
see
https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#round(double)

i want to round a double variable into two decimals only [duplicate]

This question already has answers here:
Round a double to 2 decimal places [duplicate]
(13 answers)
Closed 8 years ago.
how can i round a double variables in java into only two decimals?
yourNewFormatedDouble = new DecimalFormat("#.##").format(yourOldDoubleHere);
DecimalFormat df = new DecimalFormat("0.00##");
String result = df.format(myDouble);

Java how to divide 2 doubles with 2 digits precision? [duplicate]

This question already has answers here:
How can I truncate a double to only two decimal places in Java?
(18 answers)
Closed 8 years ago.
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?
type is double
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);

Categories

Resources