screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script src="https://unpkg.com/react@17.0.2/umd/react.development.js"></script> <script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.development.js"></script> <script src="https://unpkg.com/@babel/standalone@7.14.7/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel"> function formatNumberWithHyphen(number) { // Convert the number to a string and remove any non-digit characters const cleanNumber = String(number).replace(/\D/g, ""); // Define the number of digits in each group const digitsInGroup = 3; // Split the cleanNumber into groups of digits from right to left const groups = []; let startIndex = cleanNumber.length; while (startIndex > 0) { const endIndex = startIndex; startIndex = Math.max(0, startIndex - digitsInGroup); groups.unshift(cleanNumber.slice(startIndex, endIndex)); } // Join the groups with hyphens and return the formatted string return groups.join("-"); } function FormatNumberApp() { const numberToFormat = 1234567890; return ( <div className='container'> <h3>React js format number - hypen seperated</h3> <p>Original number: {numberToFormat}</p> <p>Formatted number: {formatNumberWithHyphen(numberToFormat)}</p> </div> ); } ReactDOM.render(<FormatNumberApp />, document.getElementById("app")); </script> <style> .container { max-width: 500px; margin: 0 auto; padding: 20px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); font-family: Arial, sans-serif; } </style> </body> </html>