Javascript-以2位小数显示总数

Javascript - Show the total in 2 decimal

本文关键字:显示 小数 2位 Javascript-      更新时间:2023-09-26

我使用的是这个脚本:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
<script language="JavaScript" type="text/javascript">
function CalculateTotal(frm) {
    var order_total = 0
    // Run through all the form fields
    for (var i=0; i < frm.elements.length; ++i) {
        // Get the current field
        form_field = frm.elements[i]
        // Get the field's name
        form_name = form_field.name
        // Is it a "product" field?
        if (form_name.substring(0,4) == "PROD") {
            // If so, extract the price from the name
            item_price = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1))
            // Get the quantity
            item_quantity = parseInt(form_field.value)
            // Update the order total
            if (item_quantity >= 0) {
                order_total += item_quantity * item_price
            }
        }
    }
    // Display the total rounded to two decimal places
    frm.TOTAL.value = round_decimals(order_total, 2)
    var total = round_decimals(order_total, 2);
    document.getElementById('tax_amount').value =round_decimals(parseFloat(((8/100)*total)),2);
    document.getElementById('total_amount').value =round_decimals((parseFloat(total) + parseFloat(((8/100)*total))),2);
}
function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(8, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(8, decimals)
    return pad_with_zeros(result3, decimals)
}
function pad_with_zeros(rounded_value, decimal_places) {
    // Convert the number to a string
    var value_string = rounded_value.toString()
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")
    // Is there a decimal point?
    if (decimal_location == -1) {
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {
        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 2
    }
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    if (pad_total > 0) {
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}
//-->
</script>
</head>
<body>
<div class="calculator">
  <div class="calc_top">
    <div class="calc_head">recruitment project - pricing calculator</div>
  </div>
  <div class="calc_body_header">
    <div class="calc_product"><strong><em>Process</em></strong></div>
    <div class="calc_cost_per_unit" style="width:140px !important;"><em><strong>Cost per Passenger</strong></em></div>
    <div class="calc_units"><strong><em>Units</em></strong></div>
  </div>
  <div class="calc_body_background">
  <br class="clearfloat" />
  <div class="calc_body">
    <div class="calc_product"><strong>Service Charges</strong></div>
    <div class="calc_units"></div>
    <div class="calc_cost_per_unit"></div>
  </div>
  <span class="clearfloat" /></span>
    
  <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="emaill@gmail.com">
<input type="hidden" name="button_subtype" value="services">
<input type="hidden" name="no_note" value="0">
<input type="hidden" name="cn" value="Enter shipping address">
<input type="hidden" name="no_shipping" value="2">
            <input type="hidden" name="item_name" value='Payment to my company'>
            <input type="hidden" name="charset" value="utf-8">
    <div class="calc_body">
      <div class="calc_product">Advertising - drafting and posting</div>
      <div class="calc_units">$8.59</div>
      <div class="calc_cost_per_unit"><span class="calc_units" style="width:140px !important;"><INPUT TYPE=TEXT NAME="PROD_ad_8.59" SIZE=3 MAXLENGTH=3 onChange="CalculateTotal(this.form)"></span></div>
      </div>
    <span class="clearfloat" /></span>
   
    
   
    <div class="calc_totals_panel_green">
      <div class="calc_totals">
        <div class="calc_total_label">TOTAL (excluding fee) $</div>
        <div class="calc_cost_per_unit"><INPUT NAME=TOTAL TYPE=TEXT class="total" onFocus="this.form.elements[0].focus()" SIZE=10 /></div>
        </div>
         <span class="clearfloat" /></span>
      <div class="calc_totals">
        <div class="calc_total_label">8 % fee</div>
        <div class="calc_cost_per_unit"><INPUT NAME="tax_amount" TYPE=TEXT class="total" id="tax_amount" SIZE=10 ></div>
        </div>
      <span class="clearfloat" /></span>
      <div class="calc_totals">
        <div class="calc_total_label">TOTAL $</div>
        <input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHosted">
        <div class="calc_cost_per_unit"><INPUT NAME="amount" TYPE=TEXT class="total" id="total_amount" SIZE=10 ></div>
        </div>
    <span class="clearfloat" /></span></div><input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_paynowCC_LG.gif" border="0" name="submit" style="vertical-align:middle" alt="PayPal">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
      </form>
  
  </div>
  
  
  
  
  
  
  
  
<div class="calc_footer">
  <div class="calc_footer_text"></div></div>
</div>
</body>
</html>

我需要收费8.59美元,再加上8%的费用。总数出来了。9.28125

当处理贝宝支付时,我收到错误,问题是贝宝不接受超过2位小数。

如何转换所有的小数点后两位。

示例:

这是错误的。

http://i271.photobucket.com/albums/jj146/cosmossx/error.jpg

这很好。http://i271.photobucket.com/albums/jj146/cosmossx/good.jpg

感谢

您可以使用javascript number.toFixed(<precision>),例如运行

var x = 1.23456
x.toFixed(2)

返回1.23