0

I wanna ask again .. hope you know I want to calculate automatically but with format money..

this demo : https://jsfiddle.net/xp4ky2gg/

This my code ...

code html

                    <table style="width:100%">
                  <thead>
                    <tr>
                      <th>Pendapatan Keluarga (A) Bersumber dari</th>
                      <th>Jumlah (Rp/Bulan)</th>
                    </tr>
                  </thead>
                  <tbody>
                      <tr>
                        <td>Usaha Pokok Suami</td>
                        <td>
                          <input type="text" name="pendapatan_suami" id="nominal1" class="pendapatan" style="width: 200px; text-align:right;"onkeyup="angka(this)" >
                        </td>
                      </tr>
                      <tr>
                        <td>Usaha Pokok Istri</td>
                        <td>
                          <input type="text" name="pendapatan_istri" id="nominal2" class="pendapatan" style="width: 200px; text-align:right;" onkeyup="angka(this)" >
                        </td>
                      </tr>
                      <tr>
                        <td>Usaha Lainnya</td>
                        <td>
                          <input type="text" name="pendapatan_usaha" id="nominal3" class="pendapatan" style="width: 200px;  text-align:right;" onkeyup="angka(this)">
                        </td>
                      </tr>
                      <tr>
                        <td>Dari orangtua</td>
                        <td>
                          <input type="text" name="pendapatan_ortu" id="nominal3" class="pendapatan" style="width: 200px; text-align:right;" onkeyup="angka(this)">
                        </td>
                      </tr>
                      <tr>
                        <td>Dari anak/menantu</td>
                        <td>
                          <input type="text" name="pendapatan_anak" id="nominal4" class="pendapatan" style="width: 200px; text-align:right;" onkeyup="angka(this)">
                        </td>
                      </tr>
                      <tr>
                        <td>Penghasilan Lainnya sebutkan</td>
                        <td><input type="text" name="pendapatan_lain" id="nominal6" class="pendapatan" style="width: 200px; text-align:right;" onkeyup="angka(this)"></td>
                      </tr>
                      <tr>
                        <td>Total</td>
                        <td>
                          <input type="text" name="total" id="total" style="width: 200px;">
                        </td>
                      </tr>                          
                      <tr>
                        <td>Kesimpulan III<span class="red">*</span></td>
                        <td>
                          <select id="k3"  class="tes3" name="kelayakan3" size="1" >
                            <option value="">-- Pilih Salah Satu --</option>
                              <option value="1">Layak</option>
                              <option value="0">Tidak Layak</option>
                          </select>
                        </td>
                      </tr>
                      <tr>
                        <td style="vertical-align:top;" class="wrap">Ket Kelayakan III</td>
                        <td><textarea name="ketkel3" maxlength="100"></textarea></td>
                      </tr>
                  </tbody>
              </table>

code javascript

      function NilaiRupiah(jumlah){  
    var titik = ".";
    var nilai = new String(jumlah);  
    var pecah = [];  
    while(nilai.length > 3)  
    {  
        var asd = nilai.substr(nilai.length-3);  
        pecah.unshift(asd);  
        nilai = nilai.substr(0, nilai.length-3);  
    }  

    if(nilai.length > 0) { pecah.unshift(nilai); }  
    nilai = pecah.join(titik);
    return nilai;  
  }

  function angka(e) {
    if (!/^[0-9]+$/.test(e.value)) {
      e.value = e.value.substring(0,e.value.length-1);
    }
  }

   $(document).ready(function () {
      //Iterate through each Textbox and add keyup event handler
      $(".pendapatan").each(function () {
          $(this).keyup(function () {
              //Initialize total to 0
              var total = 0;
              $(".pendapatan").each(function () {
                  // Sum only if the text entered is number and greater than 0
                  if (!isNaN(this.value) && this.value.length != 0) {
                      total += parseFloat(this.value);
                      $(this).css("background-color", "#FEFFB0");
                  }
                  else if (this.value.length != 0){
                      $(this).css("background-color", "red");
                  }
              });
              //Assign the total to label
              //.toFixed() method will roundoff the final sum to 2 decimal places
              jml = "Rp. " + NilaiRupiah(total) + ",00";
              $("#total").val(jml);
          });
      });
  });

this demo : https://jsfiddle.net/xp4ky2gg/ if calculate

Output Now

  9000
  9000
-------
18.000

I want to input and output like this 9000 convert to 9.000 and the result 18.000

  9.000
  9.000
---------
18.0000
TARA
  • 529
  • 1
  • 6
  • 23
  • hey tara, so what actually do you want? want to format input or output, because output seems OK. – Bharat Dec 06 '16 at 04:32
  • I want to input like this 9000 convert to 9.000 and the result 18.000 – TARA Dec 06 '16 at 04:33
  • http://stackoverflow.com/questions/1990512/add-comma-to-numbers-every-three-digits see these. and calculate by yourself instead of copy and paste. – Bharat Dec 06 '16 at 04:46

2 Answers2

1

Maybe what you mean is decorate (number format) the value, but still keep the calculation, something like this

      $('.pendapatan').focus(function(){
        var v = $(this).val().replace(/[^0-9]/, '');
        $(this).val(v);
      }).blur(function(){
        var v = NilaiRupiah($(this).val());
        $(this).val(v);
      })

https://jsfiddle.net/gn0a2h8h/

Lee
  • 3,259
  • 4
  • 21
  • 27
  • yes decorate .. thanks for correction .. anyway .. but when i'm typing .. the number format not display yaa ? – TARA Dec 06 '16 at 04:54
0

You can utilize filter for that, to pass an argument to a filter in the HTML form, we pass it with a colon after the filter name (for multiple arguments, we can simply append a colon after each argument). For example, the number filter allows us to limit the number of decimal places a number can show. To pass the argument 2, we’ll append :2 to the number filter:

 <!-- Displays: 123.46 -->
    {{ 123.456789 | number:2 }}


    <!-- Displays: 9.000 -->
    {{ 9000 | number:3 }}
Jigar7521
  • 1,549
  • 14
  • 27