Stylish Scientific Calculator - MeggiTools
Run
Toggle Theme
Share Link
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Stylish Scientific Calculator</title> <style> body { font-family: Arial, sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; background-color: #f0f0f0; margin: 0; } .calculator { background-color: #333; border-radius: 10px; box-shadow: 0 0 20px rgba(0, 0, 0, 0.1); overflow: hidden; width: 300px; } .screen { background-color: #444; color: white; font-size: 2rem; padding: 10px; text-align: right; word-wrap: break-word; overflow-wrap: break-word; min-height: 60px; } .buttons { display: grid; grid-template-columns: repeat(4, 1fr); } button { background-color: #555; border: none; color: white; cursor: pointer; font-size: 1.5rem; padding: 20px 0; transition: background-color 0.3s ease; } button:hover { background-color: #777; } button.operator { background-color: #ff9500; } button.special { background-color: #4cd964; } button.equal { background-color: #007aff; grid-column: span 1; } </style> </head> <body> <div class="calculator"> <div class="screen" id="screen">0</div> <div class="buttons"> <button type="button" class="operator">C</button> <button type="button" class="operator">±</button> <button type="button" class="operator">%</button> <button type="button" class="operator">÷</button> <button type="button">7</button> <button type="button">8</button> <button type="button">9</button> <button type="button" class="operator">x</button> <button type="button">4</button> <button type="button">5</button> <button type="button">6</button> <button type="button" class="operator">-</button> <button type="button">1</button> <button type="button">2</button> <button type="button">3</button> <button type="button" class="operator">+</button> <button type="button" class="special">π</button> <button type="button">0</button> <button type="button">.</button> <button type="button" class="equal">=</button> </div> </div> <script> document.addEventListener('DOMContentLoaded', function() { const screen = document.getElementById('screen'); const buttons = document.querySelectorAll('.buttons button'); let calculation = ''; let currentInput = ''; let lastOperator = ''; function updateScreen(value) { screen.textContent = value; } buttons.forEach(button => { button.addEventListener('click', () => { const buttonText = button.textContent; if (buttonText === 'C') { calculation = ''; currentInput = ''; lastOperator = ''; updateScreen('0'); } else if (buttonText === '±') { if (currentInput) { currentInput = (parseFloat(currentInput) * -1).toString(); updateScreen(currentInput); } } else if (buttonText === '%') { if (currentInput) { currentInput = (parseFloat(currentInput) / 100).toString(); updateScreen(currentInput); } } else if (buttonText === 'π') { currentInput = Math.PI.toString(); updateScreen(currentInput); } else if (buttonText === '=') { if (currentInput && lastOperator) { calculation += currentInput; try { calculation = eval(calculation).toString(); updateScreen(calculation); currentInput = calculation; calculation = ''; lastOperator = ''; } catch (e) { updateScreen('Error'); calculation = ''; currentInput = ''; lastOperator = ''; } } } else if (['÷', 'x', '-', '+'].includes(buttonText)) { if (currentInput) { calculation += currentInput; calculation += buttonText === '÷' ? '/' : buttonText === 'x' ? '*' : buttonText; lastOperator = buttonText; currentInput = ''; updateScreen(buttonText); } } else { currentInput += buttonText; updateScreen(currentInput); } }); }); }); </script> </body> </html>