This question already has answers here:
Double increments in Java [duplicate]
(6 answers)
Closed 9 years ago.
public static void main(String[] args) {
double test = 1;
int tick = 0;
while(tick < 10){
System.out.println(test += 1.4);
tick++;
}
}
Result:
2.4
3.8
5.199999999999999
6.6
8.0
9.4
10.8
12.200000000000001
13.600000000000001
15.000000000000002
I don't understand why It get these strange decimal numbers
like 5.19 instead of 5.2
Why is this? And is there a way to just make it appear to 5.2 like my calculator does? Thanks in advance. Marten
It's just the way double is working. It ain't perfect. What you see is rounding errors, caused by the fact that not all numbers can be accurately represented in binary.
If you want to go for precision, you should try BigDecimal instead.
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
I am using this JAVA program where I am multiplying a double with a double and storing it in a double.
My expected output is 7.14 as per my mathematics knowledge, but I get 7.140000000000001. I don't understand why is the 0000000000001 part is coming
public class Main
{
public static void main(String[] args) {
double a=7.0;
double b=1;
double c=a*b*1.02;
System.out.println(c);
}
}
So you are getting a super high level precision output for your operation. If you want to set a precision limit, you can do so following the example below
double no=12.786;
DecimalFormat dec = new DecimalFormat("#0.00");
System.out.println(dec.format(no));
This question already has answers here:
Simple division operation returning zero?
(5 answers)
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 5 years ago.
Hey people of Stackoverflow! I just have a question about an error that I came across while doing this lesson on Java online. So this is the code:
import java.util.Scanner;
public class GradesAndPoints {
public static void main(String[] args) {
System.out.print("Type in your score between (0-27): ");
Scanner ask = new Scanner(System.in);
int num = ask.nextInt();
int result = (num/27);
System.out.println(result);
The error is: whenever I run the code with the variable "num" being any int value, it prints out to be 0. Can someone explain to me why this error occurs and a solution I can implement to solve this?
The way you're doing this, you're diving integers. This, by definition, will get you an integer that is truncated.
5 / 10 = 0
If you turn one of them into a float (by adding a . at the end), you will get floating point division, which is what you're looking for.
5. / 10 = 0.5
5 / 10. = 0.5
5.0 / 10.0 = 0.5
This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 5 years ago.
public class Test {
public static void main(String[] args) {
int test = 1;
System.out.println((double)(Math.pow(test/++test, 2)));
}
0.0 is printed to the screen. Why? Why is the cast not working as expected?
If test is declared this way...
double test = 1;
I get what I expect to print... 0.25.
Why?
I am new to programming and I'm playing around. Reading some of the documentation at this level is next to useless.
You are doing integer division. SO change the line to
System.out.println((Math.pow(test/(double)++test, 2)));
to do double division
This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 6 years ago.
Every time I run this test program it display, 0.0 instead of 0.5
Does anyone know how to fix this in Eclipse?
public class Test {
public static void main(String[]args){
double distance;
distance = 1/2;
System.out.println(distance);
}
}
Your division is Integer division. There is no fault of Eclipse. Try following:
distance = 1/2.0;
OR
distance = 1.0/2;
Output:0.5
This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
How to Round Decimals to 2 Places after the Decimal Point (Java)
(4 answers)
Closed 7 years ago.
I'm new to java programming. I would like to round up a price to the nearest 2 decimal places with a multiple of 5.
Eg.
38.80 stays the same.
38.81 to 38.80.
38.82 to 38.80.
38.83 to 38.85.
38.84 to 38.85.
38.85 stays the same.
38.86 to 38.85.
38.87 to 38.85.
38.88 to 38.90.
38.89 to 38.90.
38.90 stays the same.
I tried the provided duplicates but they come out only to 1 decimal place.
Eg. 38.82 to 38.8.
This is my code:
import java.text.DecimalFormat;
public class RoundUp {
public static void main(String[] args){
DecimalFormat df = new DecimalFormat("#.##");
double num = 38.84;
System.out.println(df.format(Math.round(num*10.00)/10.00));
}
}
I have looked into other model answers by experts in this web but none of them really answer my question. Setting into 2 decimal places, I'm using DemicalFormat. That I know, but rounding the number, let's say 38.83 to 38.85 and 38.87 to 38.90 is what I really want.
It is a rounding system that my country is using. Can check it out here.
**
This question has been answer by #Mureinik double rounded =
Math.round(num * 100.0 / 5.0) * 5.0 / 100.0;
**
I would recommend you to use BigDecimal instead of double when you are dealing with money.
And then it would be like
BigDecimal value = new BigDecimal(38.84);
value = value.setScale(2, BigDecimal.ROUND_HALF_UP)
You can refer Javadocs for ROUND_HALF_UP and setScale