Java-script:去掉多余的小数位

Posted

技术标签:

【中文标题】Java-script:去掉多余的小数位【英文标题】:Java-script: getting rid of extra decimal places 【发布时间】:2019-06-04 17:20:14 【问题描述】:

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

小数点后三位

这一切都在 javascript 部分代码底部的“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>

这里是 ABOVE 部分的代码 以及整个代码 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"  id="Y">Yes</th>                   
            <th class="ttable"  id="N">No</th>                    
            <th class="ttable"  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>

【问题讨论】:

JavaScript 数字是 二进制浮点 数字。在任何语言中用二进制浮点数进行“金钱数学”通常被认为是一个坏主意,因为并非所有小数都可以精确表示。 您是否使用了正确的大小写 toFixed()? JavaScript 区分大小写,并且您问题中的确切大小写不会起作用。试试toFixed(2) 就像那样。 ***.com/questions/11832914/… 确保您通过浏览器上的开发人员工具查看控制台以捕获此类错误。 JavaScript 没有连字符。就一个字。 【参考方案1】:

应该是 固定(2) 不是 固定(2)

【讨论】:

我的复制和粘贴是这样做的,但是我知道它已修复 :),现在只需要知道如何解决问题【参考方案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)”来查看您正在使用的数据类型。

【讨论】:

【参考方案3】:

你有没有试过这个:

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

【讨论】:

你看到我在上面说这个的部分了吗?我已经检查了关于这个问题的所有重复问题,并按照说明尝试了 Tofixed(2),但是当我输入它时没有工作 1) var max = (principal/2)-fees.Tofixed(2); AND 2)text = text + '当前账户费用为 $' +fees+ '。允许的最高附加费为 $' +max.toFixed(2); 我没有对此进行测试,但没有必要在 var max = (principal/2)-fees.toFixed(2) 和 ... is $' +max.toFixed 中修复小数(2)。尝试在其中一个或另一个中使用 toFixed 但不能同时使用...?

以上是关于Java-script:去掉多余的小数位的主要内容,如果未能解决你的问题,请参考以下文章

.net 如何去掉小数点后多余的0

javascript 去掉小数末尾多余的零

java正则去掉小数点后多余0

sqlserver怎么把小数点后面多余的0去掉

sqlserver怎么把小数点后面多余的0去掉

sqlserver把小数点后面多余的0去掉