Javascript:摆脱多余的小数位

时间:2019-01-09 21:18:17

标签: javascript web-developer-toolbar

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

enter image description here

有3个小数位

所有这些都在JavaScript部分的代码底部的“ max”变量中

我已经检查了有关此问题的所有重复问题,并按照说明尝试了toFixed(2),但是当我输入为{p>时却无法正常工作

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);

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

enter image description here

<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>

此处以上部分的代码 ,并且代码末尾的以下 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>

3 个答案:

答案 0 :(得分:4)

那是     固定(2) 不     固定(2)

答案 1 :(得分:1)

您是否已在操作值之前检查了值的类型?

我改变了

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

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

在repl.it上获得了那些结果

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

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

答案 2 :(得分:0)

Have you try this:

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