I have created a program that asks the user for an amount in euros and calculates the number of bills needed to cover that amount and the rest in coins of 1, 2, 0.5, 0.20, 0.10, 0.05, 0.02, 0.01. The types of bills are: 500, 200, 100, 50, 20, 10 and 5.
The fact is that it works but not quite right because if I put for example 257.27 it returns me: There are 1.0 bills of: 200.0 Euros There are 1.0 bills of: 50.0 Euros There are 1.0 bills of: 5.0 Euros There are 1.0 coins of: 2.0 Euros There are 1.0 coins of: 0.2 Euros There are 1.0 coins of: 0.05 Euros There are 1.0 coins of: 0.01 Euros
Where we see that 1 cent of a euro is missing or what is the same, there should have been 2 coins of 2 cents (0.02)
I attach my code to see if anyone sees what I'm doing wrong.
Thank you very much in advance and I hope it will be of some use to you and you like it, greetings.
import java.util.*;
public class Euroscompleto {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Declaro variable que contendra el valor a devolver
double importe = 0;
do {
try {
System.out.print("Ingresa el cambio a devolver:");
importe = sc.nextDouble();
} catch (Exception e) {
System.out.println("Introduciste un dato erroneo.");
System.out.println("");
sc.nextLine();
}
}
while (importe <= 0);
calcular(importe);
sc.close();
}
// Método para calcular
public static void calcular(double importe) {
// Indicamos todas las monedas posibles
double[] monedas = { 500, 200, 100, 50, 20, 10, 5, 2, 1, 0.5, 0.20, 0.10, 0.05, 0.02, 0.01 };
// Creamos un array con 0 de longitud igual a la cantidad de monedas
// Este array contendra las monedas a devolver
double[] devolver = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
// Recorremos todas las monedas
for (int i = 0; i < monedas.length; i++) {
// Si el importe actual, es superior a la moneda
if (importe >= monedas[i]) {
// obtenemos cantidad de monedas
devolver[i] = Math.floor(importe / monedas[i]);
// actualizamos el valor del importe que nos queda por didivir
importe = importe - (devolver[i] * monedas[i]);
}
}
// Bucle para mostrar el resultado
for (int i = 0; i < monedas.length; i++) {
if (devolver[i] > 0) {
if (monedas[i] > 2) {
// Indicamos que es un billete
System.out.println("Hay " + devolver[i] + " billetes de: " + monedas[i] + " Euros");
} else {
// Indicamos que es una moneda
System.out.println("Hay " + devolver[i] + " monedas de: " + monedas[i] + " Euros");
}
}
}
}
}
When you work with operations with decimal numbers you have to be careful, this is because the computer performs the calculations in binary, it prints the value of
importe
so you can verify what the result isThe value of the first output should be:
57.27
but instead it returns57.26999999999998
The solution for this case is to use BigDecimal
Change the following line of code
For these lines of code
I just uploaded the Complete Euros program to GitHub after quite a few tests, but if you find bugs or improvements that I didn't think of, you can tell me and I'll keep tweaking it.
Many thanks to Joshin, for the contribution to the cent bug fixed with BigDecimal.
I share the link so you can download it.
https://github.com/JRobertGutierrez/DAM1-PROG-U1/blob/main/Unidad1/src/Euroscompleto.java
All the best.