SHARE
TWEET

js

a guest Aug 29th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7.     <title>Document</title>
  8. </head>
  9. <body>
  10.     Total<input type="text" id="total" name="total" value="10000" onkeyup="sum();" >
  11.     Bayar<input type="number" id="bayar" name="bayar" onkeyup="sum();">
  12.     Kembali<input type="text" id="rupiah" readonly>
  13.  
  14.  
  15. <script type="text/javascript">
  16.     function sum() {
  17.         var total = document.getElementById('total').value;
  18.         var bayar = document.getElementById('bayar').value;
  19.         var result = parseInt(bayar) - parseInt(total);
  20.         if (!isNaN(result)) {
  21.           document.getElementById('rupiah').value = formatRupiah(new String(result), 1);
  22.         }
  23.     }
  24.     // var rupiah = document.getElementById('rupiah');
  25.     // rupiah.addEventListener('keyup', function(e){
  26.     //  // tambahkan 'Rp.' pada saat form di ketik
  27.     //  // gunakan fungsi formatRupiah() untuk mengubah angka yang di ketik menjadi format angka
  28.     //  rupiah.value = formatRupiah(this.value, 'Rp. ');
  29.     // });
  30.  
  31.     /* Fungsi formatRupiah */
  32.     function formatRupiah(angka, prefix){
  33.         var number_string = angka.replace(/[^,\d]/g, '').toString(),
  34.         split           = number_string.split(','),
  35.         sisa            = split[0].length % 3,
  36.         rupiah          = split[0].substr(0, sisa),
  37.         ribuan          = split[0].substr(sisa).match(/\d{3}/gi);
  38.  
  39.         // tambahkan titik jika yang di input sudah menjadi angka ribuan
  40.         if(ribuan){
  41.             separator = sisa ? '.' : '';
  42.             rupiah += separator + ribuan.join('.');
  43.         }
  44.  
  45.         rupiah = split[1] != undefined ? rupiah + ',' + split[1] : rupiah;
  46.         return prefix == undefined ? rupiah : (rupiah ? 'Rp. ' + rupiah : '');
  47.     }
  48. </script>
  49. </body>
  50. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top