2

the problem I’m having is that my result base on the picture below is showing 3 decimal : $242.075 >instead of $242.07 (number at the end of the picture)

enter image description here

There 3 decimal places

It all in the “max” variable at the bottom of the code in JavaScript section

I have checked all duplicate question regarding this issue and have tried toFixed(2) as instructed but didn't work when I type it as

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

I didn't get any error but the pop screen won’t show, it just remains on the screen :(.

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>

here the code for the section ABOVE and the WHOLE code BELOW Java script part at end of the code

<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
    JavaScript numbers are *binary floating-point* numbers. Doing "money math" in binary floating point in *any* language is generally thought of as a bad idea, because not all decimal fractions can be exactly represented. – Pointy Jan 09 '19 at 21:22
  • 1
    Did you use `toFixed()` with the correct case? JavaScript is case sensitive, and the exact capitalisation in your question would not have worked. Try `toFixed(2)` exactly like that. – Jack Bashford Jan 09 '19 at 21:22
  • 2
    https://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-only-if-necessary – Bibberty Jan 09 '19 at 21:23
  • Make sure you look at the console via developer tools on your browser to catch errors like this. – Namaskar Jan 09 '19 at 21:26
  • 1
    JavaScript is not hyphenated. It's one word. –  Jan 09 '19 at 21:37

3 Answers3

4

It would be toFixed(2) not Tofixed(2)

stever
  • 1,232
  • 3
  • 13
  • 21
1

Have you checked the type of the value before operating on it?

I changed

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

to

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

on repl.it and got those results

also, for the second part, data type is the reason it is not showing up ( just tested on repl.it again ). toFixed() specifically turns a number into a string. So once the variable max is already a string, calling ' toFixed() ' on it again is an invalid operation

For future reference go to https://repl.it/languages and use the javascript sandbox to test out these sort of short-term operations on values. You can also perform a " console.log( typeof variableName ) " to see what data type you are working with.

Erik White
  • 1,016
  • 1
  • 11
  • 13
0

Have you try this:

text = text + ' Current fee on account is $' +fees+ '. Maximum additional fee allowed is $' +max.toFixed(2);
  • did you see the part where i said this atop I have checked all duplicate question regarding this issue and have tried Tofixed(2) as instructed but didn't work when I type it as 1) var max = (principal/2)-fees.Tofixed(2); AND 2)text = text + ' Current fee on account is $' +fees+ '. Maximum additional fee allowed is $' +max.toFixed(2); <<< – Mauricio Cuervo Jan 09 '19 at 22:12
  • I haven't tested this but it's not necessary to fix the decimals both in var max = (principal/2)-fees.toFixed(2) and ... is $' +max.toFixed(2). Try using toFixed in one or the other but not both...? – stever Jan 09 '19 at 22:29