Java beginner doubt-why is the result of the double variable wrong? [duplicate]
this question already has answers here :
What is the correct way to use float, double, and decimal types?
(2 responses)
inaccurate result in calculation with broken numbers
(3 responses)
Closed 1 year ago.
I made an algorithm that simulates a bank account, where the client can withdraw and deposit values, worked perfectly when I used variables of type int
, but when I use variables of type double (for the purpose of the algorithm to make counts with Cents) it returns some values that I think are wrong, for example:
public static void main(String[] args) {
double valor1 = 10;
double valor2 = 9.8;
double resultado = valor1 - valor2;
System.out.println(resultado);
The value it returns to me is 0.1999999999999993
, when I think the correct would be 0.2
0
Author: Vinicius Macedo, 2019-08-04