Java-脚本:去掉多余的小数位

Java-script: getting rid of extra decimal places

我遇到的问题是我基于下图的结果显示小数点后三位:$242.075 > 而不是 $242.07(图片末尾的数字)

有3位小数

全部在Java脚本部分

代码底部的“max”变量中

我已经检查了所有与此问题相关的重复问题,并已按照说明尝试了 toFixed(2),但当我输入

时没有成功

1) var max = (principal/2)-fees.toFixed(2); 和 2)text = text + ' Current fee on account is $' +fees+ '. Maximum additional fee allowed is $' +max.toFixed(2);

我没有收到任何错误,但弹出屏幕不会显示,它只是保留在屏幕上:(。

<script>
    function CollFee(input, principal, fees){

        var max = (principal/2)-fees;
        //var input = this.value;
        //alert(input);

        if(input.value > max){
            input.value = '';
            var text = 'Collection Fee is too high, the total of all collection fees can only be 50% of the Original Principal.';
            text = text + ' Current fee on account is $' +fees+ '. Maximum additional fee allowed is $' +max;
            alert(text);
            //document.getElementById('collfee_<?php echo $dbid; ?>').value = '';
            //input.value = '';

        }
    };
</script>

这里是以上部分的代码 和代码末尾的整个代码 BELOW Java 脚本部分

<center>
<h1>
Collection Authorization
</h1>
<div class="homemess2" align="center" >
<p class="homemess3">
Please mark each account listed below with a <b>Yes</b> if you <b><u>approve</u></b> it to be placed into our Phase II (Contingency Collections) department.
</p>
<p class="homemess3">
Please mark each account listed below with a <b>No</b> if you <b><u>do not approve</u></b> it to be placed into our Phase II (Contingency Collections) department.
</p>
<p class="homemess3">
Accounts left unmarked will remain on the Collection Authorization List.
</p>
<p class="homemess3">
NEW FEATURE: Now you can authorize accounts to go to Phase II that were 'Previously Declined', simply choose the 'Previously Declined' option in the below menu to see the Phase I accounts that can still be sent to Phase II.
</p>
</div><br>
</center>
<center style="padding-bottom:12px;">
<b>New Feature : </b>
<select id="dynamic_select">
    <option value="rep_collauthorization.php" selected="selected">Collection Authorization</option>
    <option value="rep_collauthorization_declined.php">Previously Declined</option>
    <option value="rep_collauthorization_combined.php">Combined</option>
</select>
</center>
<form method="post" action="rep_collauthorizationx.php">
<table class="ttable sort_ttable1" id="addsearchres" summary="Address Search">
    <caption class="ttable">Collection Authorization List</caption>
    <thead class="ttable">
        <tr class="ttable">
            <th class="ttable" width="30" id="Y">Yes</th>                   
            <th class="ttable" width="30" id="N">No</th>                    
            <th class="ttable" width="100" id="CR">Collection Fee</th>                      
            <th class="ttable" id="LN">File</th>                        
            <th class="ttable" id="LS">Debtor</th>                      
            <th class="ttable" id="LS">Responsible</th>                     
            <th class="ttable" id="NB">City</th> 
            <th class="ttable" id="NB">St</th>
            <th class="ttable" id="NB">Original Principal</th>
            <th class="ttable" id="NB">Balance</th>
        </tr>
    </thead>                
    <tbody class="ttable">
            <?php 
                $tprisec = "tprimary";

                $anumberaccount = "";  
                $numberaccount = 0; 

                include './includes/dblogin.php';   

                $sql_stmt = generate_coll_authorization_list();

                $qresult = mysql_query($sql_stmt);

                if (!$qresult) {
                    print(mysql_error());  
                }

                if ($qresult && mysql_num_rows($qresult) > 0 ) {   
                    echo "<input type='hidden' name='client_rowid' value='".$client_rowid."'"; 
                    while ($row = mysql_fetch_array($qresult, MYSQL_ASSOC)) { 
                        $dbfees = 0;
                        $dbid = $row["de_number"];
                        $dbcontact = $row["de_contact"]; 
                        $dbaddress = $row["de_address"];
                        $dbcity = $row["de_city"];
                        $dbstate = $row["de_state"];
                        $dbzip = $row["de_zip"];
                        $dbrname = $row["de_name"];
                        $dblisted = $row["de_listed_date"]; 
                        $dbaccount = substr($row["de_cl_account"], 0, 15);  
                        $dbprincipal = $row["de_principal"];
                        $dp= substr($dbprincipal, -2, 2);

                        if ($dp=='00'){

                            $dbprincipal= substr ($dbprincipal, 0, -2);

                        }

                        $dbfees = $row["de_fees"];
                        $dbowing = $row["de_owing"];
                        $dow= substr($dbowing, -2, 2);

                        if ($dow=='00'){

                            $dbowing= substr ($dbowing, 0, -2);

                        }



                        $dbstatus = substr($row["de_statusnew"], 0, 3);  

                        echo "<tr class='".$tprisec."'>";
                        echo "<td class='ttablex' align='center'>";
                        echo "<input type='radio' name='".$dbid."' value='Yes'";
                        echo "</td>";
                        echo "<td class='ttablex' align='center'>";
                        echo "<input type='radio' name='".$dbid."' value='No'"; 
                        echo "</td>";
                        echo "<td class='ttablex' align='center'>";
                        //echo "<input class='collfee' onchange='CollFee(this.value, '$dbprincipal', '$dbfees')' id='collfee_".$dbid."' type='text' name='cf_".$dbid."' size='8'>";
                        ?>
                        <input class='collfee' onchange="CollFee(this, '<?php echo $dbprincipal; ?>', '<?php echo $dbfees; ?>')" id='collfee_<?php echo $dbid;?>' type='text' name='cf_<?php echo $dbid;?>' size='8'>
                        <?php
                        echo "</td>";
                        echo "<td class='ttablex' align='right'>".$dbid."</td>";
                        echo "<td class='ttablex' align='left'>".$dbcontact."</td>"; 
                        echo "<td class='ttablex' align='left'>".$dbrname."</td>"; 
                        echo "<td class='ttablex' align='left'>".$dbcity."</td>";
                        echo "<td class='ttablex' align='left'>".$dbstate."</td>";
                        echo "<td class='ttablex' align='right'>".$dbprincipal."</td>";
                        echo "<td class='ttablex' id='principal_amt' align='right'>".$dbowing;
                        echo "<input id='pamt_".$dbid."' class='pamt' type='hidden' name='pamt_".$dbid."' value=".$dbprincipal.">";
                        echo "</td>";
                        echo "</tr>";

                        if ($tprisec == "tprimary") {
                            $tprisec = "tsecondary";
                        } else {
                            $tprisec = "tprimary";
                        }
                        $numberaccount ++; 
                    }
                        $anumberaccount = number_format($numberaccount,0); 
                    }

                    echo "</tbody>";
                echo "</table>";
                $anumberaccount = number_format($numberaccount,0); 
                if($anumberaccount == '1') {
                    echo "<br>There is ".$anumberaccount." account.<br><br>";
                } else {
                    echo "<br>There are ".$anumberaccount." accounts.<br><br>";
                }           
            ?>
    <center>
    <input type="submit" name="terms" id="terms" value="Terms and Conditions"><br>
    <input type="checkbox" name="readagreed" id="readagreed" value="terms">I have read and agree to the Terms and Conditions.<br><br><br>
    <input type="submit" name="submit" id="submit" value="Submit">&nbsp;&nbsp;
    <input type="reset" value="Reset"><br>
    </center>
    <input type="submit" name="submit_real" id="submit_real">
</form>
<script>
    function CollFee(input, principal, fees){

        var max = (principal/2)-fees;
        //var input = this.value;
        //alert(input);

        if(input.value > max){
            input.value = '';
            var text = 'Collection Fee is too high, the total of all collection fees can only be 50% of the Original Principal.';
            text = text + ' Current fee on account is $' +fees+ '. Maximum additional fee allowed is $' +max;
            alert(text);
            //document.getElementById('collfee_<?php echo $dbid; ?>').value = '';
            //input.value = '';

        }
    };
</script>

会是 到固定(2) 不是 固定(2)

你试过这个吗:

text = text + ' Current fee on account is $' +fees+ '. Maximum additional fee allowed is $' +max.toFixed(2);

你在操作之前检查过值的类型吗?

我变了

var max = (principal/2)-fees.toFixed(2); // 0.5

var max = ( (principal/2)-fees ).toFixed(2); // 0.50

在 repl.it 上得到了这些结果

另外,对于第二部分,数据类型是它没有显示的原因(刚刚在 repl.it 上再次测试)。 toFixed() 专门将 number 转换为 string。因此,一旦变量 max 已经是一个字符串,再次对其调用 ' toFixed() ' 是无效操作

为了将来参考,请转到 https://repl.it/languages 并使用 javascript 沙箱来测试这些对值的短期操作。您还可以执行“console.log(typeof variableName)”来查看您正在使用的数据类型。