This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
double basicPay = 1999.0;
double hra = 145.0;
float experience = 3.0f;
double percentage;
if(experience<3)percentage = 0;
else if(experience>=3 && experience<5)percentage = 5;
else if(experience>=5 && experience<10)percentage = 7;
else percentage = 12;
double salary = (basicPay + hra + basicPay*(percentage/100));
System.out.println(salary);
This shows output as 2243.95
But if I change
double salary = (float)(basicPay + hra + basicPay*(percentage/100));
The answer now is 2243.949951171875
Why is this happening?
This is problem not related to Java, but to the standard of floating point numbers and their precision. Not all numbers can be represented with float. Instead of the exact value the floating point is going to get a value that is a closest approximation of the real value in floating point format. double has much more bytes to store the data, so it's more precise.
I won't go into explaining the whole thing because I believe there is plenty of resources which explain it very well, google floating point precision.
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
When I divided the large number into a small number then the division is correct but when I write the small number to divide a large number the answer returns wrong. In my scenario, the small number always be first. here is my code this code return 7.4074074074074075E-6 but the correct result is 0.0000074074.
double itf = 0.0;
double a = 4.0;
double b = 540000;
itf = a / b;
Log.i(TAG, "savedata: outputvalue=" + itf);
BigDecimal a = new BigDecimal("4");
BigDecimal b = new BigDecimal("540000");
// 0.0000074074
a.divide(b, MathContext.DECIMAL128);
You should use a decimal type. double is outside the scope of support
This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 2 years ago.
I am trying to show the percentage of day passed using a fixed time. However, when I divide the time passed already by the total amount of time (in seconds) of a day, I get 0.0. I put the current values into the console. Any help is appreciated.
You are performing integer division, and then casting it to a double. You should be doing:
int numOfSecondsSinceMidnight = 61960;
int totalDay = 86400;
double percentDayPassed = 0;
percentDayPassed = (((double)numOfSecondsSinceMidnight / totalDay)*100);
System.out.println(percentDayPassed);
Or better yet, changing numOfSecondsSinceMidnight and totalDay to doubles:
double numOfSecondsSinceMidnight = 61960;
double totalDay = 86400;
double percentDayPassed = 0;
percentDayPassed = ((numOfSecondsSinceMidnight / totalDay)*100);
System.out.println(percentDayPassed);
Both of which print:
71.71296296296296
This question already has answers here:
Retain precision with double in Java
(24 answers)
Is floating point math broken?
(31 answers)
Closed 4 years ago.
Eclipse gives the wrong result when trying to calculate the sum of two floats.
In my code, there are 2 float variables: float from = 0.025 and float to = 1.
Then result has double variable: double value = 7 * from / to.
Eclipse compiler shows: value = 0.174999997019767760
In excel calculator, this result was value = 0.175
How can I solve this an issue?
This is simply due to your Java program not rounding the result the same way the excel calculator does. This is a result of the way computers handle floating point arithmetic. You have two options: round the result, or use the java BigDecimal class. If you want to round the result, you can use:
float from = 0.025f;
float to = 1;
double value = 7 * from / to;
DecimalFormat ds = new DecimalFormat("#.###");
double rounded = Double.parseDouble(ds.format(value));
System.out.println(rounded);
If you would rather not have to round, you can use the BigDecimal class.
Java BigDecimal
This question already has answers here:
How do I get the part after the decimal point in Java?
(15 answers)
Closed 6 years ago.
I am developing an app in which i have to get the float part in terms of integer value.
for example if my number is 153.12324 then output should be 12324.I tried this way but it works wrong some time.
What's the problem in this or is there a better way to do this ?
there is a double value in d
double d =any_double_value;
String[] splitter = String.valueOf(d).split("\\.");
splitter[0].length(); // Before Decimal Count
int count = splitter[1].length();
int integerpart = (int) calcResult;
double floatpart = calcResult - integerpart;
while (count!=0)
{
floatpart=floatpart*10;
count--;
}
int floatpartinInt=(int)floatpart;
this works wrong only in some cases(it gives 1 number less, like if answer is 124 it gives 123) and in cases where answer is long double no like 3.333333333 (10/3)
You can also without split. by using following way,
double value = 3.25;
double fractionalPart = value % 1;
double integralPart = value - fractionalPart;
This may helps you
Use this code its work in your case and enjoy Man
double val=1.9;
String[] arr=String.valueOf(val).split("\\.");
int[] intArr=new int[2];
intArr[0]=Integer.parseInt(arr[0]); // 1
intArr[1]=Integer.parseInt(arr[1]); // 9
You could also do this:
lvalue = (long) value;
fPart = value - lvalue;
This question already has answers here:
How do I get whole and fractional parts from double in JSP/Java?
(18 answers)
Closed 9 years ago.
double d = 4.321562;
Is there an easy way to extract the 0.321562 on it's own from d? I tried looking in the math class but no luck. If this can be done without converting to string or casting to anything else, even better.
Well, you can use:
double x = d - Math.floor(d);
Note that due to the way that binary floating point works, that won't give you exactly 0.321562, as the original value isn't exactly 4.321562. If you're really interested in exact digits, you should use BigDecimal instead.
Another way to get the fraction without using Math is to cast to a long.
double x = d - (long) d;
When you print a double the toString will perform a small amount of rounding so you don't see any rounding error. However, when you remove the integer part, the rounding is no longer enough and the rounding error becomes obvious.
The way around this is to do the rounding yourself or use BigDecimal which allows you to control the rounding.
double d = 4.321562;
System.out.println("Double value from toString " + d);
System.out.println("Exact representation " + new BigDecimal(d));
double x = d - (long) d;
System.out.println("Fraction from toString " + x);
System.out.println("Exact value of fraction " + new BigDecimal(x));
System.out.printf("Rounded to 6 places %.6f%n", x);
double x2 = Math.round(x * 1e9) / 1e9;
System.out.println("After rounding to 9 places toString " + x2);
System.out.println("After rounding to 9 places, exact value " + new BigDecimal(x2));
prints
Double value from toString 4.321562
Exact representation 4.321562000000000125510268844664096832275390625
Fraction from toString 0.3215620000000001
Exact value of fraction 0.321562000000000125510268844664096832275390625
Rounded to 6 places 0.321562
After rounding to 9 places toString 0.321562
After rounding to 9 places, exact value 0.32156200000000001448796638214844278991222381591796875
NOTE: double has limited precision and you can see representation issue creep in if you don't use appropriate rounding. This can happen in any calculation you use with double esp numbers which are not an exact sum of powers of 2.
Use modulo:
double d = 3.123 % 1;
assertEquals(0.123, d,0.000001);