-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculadora-de-dolares-antigo.html
100 lines (95 loc) · 3.73 KB
/
calculadora-de-dolares-antigo.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculadora de Conversão para Dólar e Real</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.calculator {
background-color: white;
padding: 50px;
border-radius: 10px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
width: 100%;
max-width: 400px;
}
input, button, select {
padding: 10px;
margin-top: 10px;
width: 100%;
font-size: 16px;
box-sizing: border-box;
}
button {
background-color: #007BFF;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
.result {
margin-top: 15px;
font-size: 18px;
}
.info {
margin-top: 10px;
font-size: 14px;
color: #555;
}
</style>
</head>
<body>
<div class="calculator">
<h2>Conversor de dólar hoje</h2>
<select id="conversionType">
<option value="BRLtoUSD">Converter de Real para Dólar</option>
<option value="USDtoBRL">Converter de Dólar para Real</option>
</select>
<input type="number" id="valor" placeholder="Insira o valor">
<button onclick="converterMoeda()">Converter</button>
<div class="result" id="resultado"></div>
<div class="info" id="info"></div>
</div>
<script>
async function converterMoeda() {
const valor = document.getElementById('valor').value;
const conversionType = document.getElementById('conversionType').value;
if (valor === '') {
alert('Por favor, insira um valor.');
return;
}
try {
const response = await fetch('https://economia.awesomeapi.com.br/last/USD-BRL');
const data = await response.json();
const taxaDeCambio = parseFloat(data.USDBRL.bid);
const dataCotacao = new Date(data.USDBRL.create_date).toLocaleString('pt-BR');
let resultado;
if (conversionType === "BRLtoUSD") {
const valorEmDolar = (valor / taxaDeCambio).toLocaleString('pt-BR', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
resultado = `R$ ${valor} equivale a $ ${valorEmDolar}`;
} else {
const valorEmReais = (valor * taxaDeCambio).toLocaleString('pt-BR', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
resultado = `$ ${valor} equivale a R$ ${valorEmReais}`;
}
const taxaDeCambioFormatada = taxaDeCambio.toLocaleString('pt-BR', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
document.getElementById('resultado').textContent = resultado;
document.getElementById('info').textContent = `Cotação atual: $ ${taxaDeCambioFormatada} (Atualizado em: ${dataCotacao}) \nFonte: AwesomeAPI (https://economia.awesomeapi.com.br)`;
} catch (error) {
document.getElementById('resultado').textContent = 'Erro ao obter a taxa de câmbio. Tente novamente mais tarde.';
document.getElementById('info').textContent = '';
}
}
</script>
</body>
</html>