-
Notifications
You must be signed in to change notification settings - Fork 29
/
KmtoMetersAndMilesConverter.html
68 lines (62 loc) · 1.84 KB
/
KmtoMetersAndMilesConverter.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>KM to Meters and Miles Converter</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 600px;
margin: 0 auto;
padding: 20px;
}
.converter {
background-color: #f4f4f4;
border-radius: 5px;
padding: 15px;
margin-bottom: 10px;
}
input[type="number"] {
width: 100%;
height: 30px;
margin-right: 10px;
}
button {
background-color: #007bff;
color: white;
border: none;
cursor: pointer;
padding: 5px 10px;
border-radius: 3px;
}
button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<h2>Kilometers to Meters and Miles Converter</h2>
<div class="converter">
<input type="number" id="kmInput" placeholder="Enter kilometers">
<button onclick="convertKm()">Convert</button>
</div>
<p>Result:</p>
<div id="result"></div>
<script>
function convertKm() {
const km = parseFloat(document.getElementById('kmInput').value);
if (isNaN(km) || km < 0) {
document.getElementById('result').textContent = 'Please enter a valid positive number';
return;
}
const meters = km * 1000;
const miles = km * 0.621371;
document.getElementById('result').innerHTML =
`<strong>${km.toFixed(2)} km is equal to:</strong><br>`
+ `${meters.toFixed(2)} meters<br>`
+ `${miles.toFixed(2)} miles`;
}
</script>
</body>
</html>