如何将小数舍入到2位?

时间:2018-06-06 19:15:41

标签: javascript html

下面,我正在使用这个JS:

        $("input[name='AuthorizedAmount'], input[name='LessLaborToDate']").change(function () {
        var sum = parseFloat($("input[name='AuthorizedAmount']").val()).toFixed(2) - parseFloat($("input[name='LessLaborToDate']").val()).toFixed(2);
        $("input[name='Subtotal'").val(sum).toFixed(2);
    });

当我从LessLaborToDate中减去AuthorizedAmount时,在小计框中出现的数字在小数点后面有很多浮点数。下面是方框的HTML:

                                    <div class="box-body">

                                        <div class="row">

                                            <div class="col-md-6">
                                                <div class="form-group">
                                                    <label>Authorized Amount</label>
                                                    <div class="input-group">
                                                        <span class="input-group-addon">$</span>
                                                        <input type="number" class="form-control" name="AuthorizedAmount" min="0" max="9999999999.99" step="0.01" required>

                                                    </div>
                                                </div><!-- /.form-group -->
                                            </div><!-- /.col -->

                                            <div class="col-md-6">
                                                <div class="form-group">
                                                    <label>Less Labor to Date</label>
                                                    <div class="input-group">
                                                        <span class="input-group-addon">$</span>
                                                        <input type="number" value="0" class="form-control" name="LessLaborToDate" min="0" max="9999999999.99" step="0.01" required>
                                                    </div>
                                                </div><!-- /.form-group -->
                                            </div><!-- /.col -->

                                        </div><!-- /.row -->

                                        <div class="row">

                                            <div class="col-md-6">
                                                <div class="form-group">
                                                    <label>Subtotal</label>
                                                    <div class="input-group">
                                                        <span class="input-group-addon">$</span>
                                                        <input type="number" class="form-control" name="Subtotal" placeholder="Authorized Amount minus Less Labor to Date" min="0" max="9999999999.99" step="0.01" readonly>
                                                    </div>
                                                </div><!-- /.form-group -->
                                            </div><!-- /.col -->

                                            <div class="col-md-6">
                                                <div class="form-group">
                                                </div>
                                            </div><!-- /.col -->

                                        </div><!-- /.row -->

                                    </div><!-- /.box-body -->

我做错了什么不能只显示2位小数?

1 个答案:

答案 0 :(得分:1)

更改此行:

<link href="https://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet"/>


<div class="container-fluid bg-grey pb-5 ">
  <div class="row">
    <div class="container">
      <!-- second part -->


      <div class="row pt-5">

        <div class="d-md-flex flex-md-equal w-100 ">

          <div class="col-8 bg-white mr-md-3 pt-3 px-3 pt-md-5 px-md-5 overflow-hidden">
            <div class="my-3 py-3">
              <h3 class="heading03">Life </h3>
              <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque
                eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer
                tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus.
              </p>
            </div>
          </div>


          <div class="col-4 bg-dark mr-md-3 pt-3 px-3 pt-md-5 px-md-5 text-white overflow-hidden">
            <div class="col-12">
              <div class="my-3 p-3">
                <h2 class="display-5"> headline</h2>
                <p class="lead">subheading.</p>
              </div>

            </div>


          </div>
        </div>

      </div>


    </div>
  </div>
</div>

对此:

//This is calling toFixed() on the value of input[name='Subtotal]
$("input[name='Subtotal'").val(sum).toFixed(2);