2

我正在使用 Pengoworks jquery 计算器并且不希望我的总和被四舍五入。即:如果总和为 19.99,则四舍五入为 19。

我“认为”它在这里:“$”+ sum.toFixed(2)

完整代码在这里:http ://www.pengoworks.com/workshop/jquery/calculation/calculation.plugin.htm

完整代码

<script type="text/javascript">
    var bIsFirebugReady = (!!window.console && !!window.console.log);

    $(document).ready(
        function (){
            // update the plug-in version
            $("#idPluginVersion").text($.Calculation.version);

/*
// test precision
$("body").prepend("<div id='nn'></div>");
$("#nn").calc(
    // the equation to use for the calculation
    "qty * price",
    // define the variables used in the equation, these can be a jQuery object
    {
        qty: 23,
        price: 1.4
    }   
);
*/

/*          
            $.Calculation.setDefaults({
                onParseError: function(){
                    this.css("backgroundColor", "#cc0000")
                }
                , onParseClear: function (){
                    this.css("backgroundColor", "");
                }
            });
*/

            /*$("#qty_1082").keyup(function(){
                var dInput = $(this).val();
        alert(dInput);
            });*/

            /* ADDITION FOR VARIABLE DISCOUNTS - START */

            //bind check_discount on key up - MUST BE BEFORE recalc Function
            $("input[name^=qty_item_]").bind("keyup", check_discount);
            check_discount();
        /*  $( "input[name^=qty_item_]" ).each(function() {
                alert(this.name);
            });*/


            function check_discount(){
                $( "input[name^=qty_item_]" ).each(function() {
                    var input_value = $(this).val(); //get the value (amount of books) for the current input field being modified
                    var current_input_id = this.name; //get the name field for the current input field being modified   
                    var current_price=current_input_id.replace("qty","price"); //swap text to make it match the mathing price field


                    //Series of staments to determine the appropriate action
                    if(input_value < 10){
                        //alert("There is a minimum order of 10 books per title");
                        $("#"+current_price).text("$0.00"); //If between 50 and 99 adjust rate

                    } else if (input_value >= 10 && input_value <=49){
                        //alert("Between 10 - 49");
                        $("#"+current_price).text("$7.46"); //If between 50 and 99 adjust rate

                    } else if (input_value >= 50 && input_value <=99){
                        //alert("Between 50 - 99");
                        $("#"+current_price).text("$5.97"); //If between 50 and 99 adjust rate

                    } else if (input_value >= 100 ){
                        //alert("Over 100");
                        $("#"+current_price).text("$5.47"); //If over 100 adjust rate
                    } else{

                    }

                });

            }

            /* ADDITION FOR VARIABLE DISCOUNTS - END */


            // bind the recalc function to the quantity fields
            $("input[name^=qty_item_]").bind("keyup", recalc);
            // run the calculation function now
            recalc();

            // automatically update the "#totalSum" field every time
            // the values are changes via the keyup event
            $("input[name^=sum]").sum("keyup", "#totalSum");

            // automatically update the "#totalAvg" field every time
            // the values are changes via the keyup event
            $("input[name^=avg]").avg({
                bind:"keyup"
                , selector: "#totalAvg"
                // if an invalid character is found, change the background color
                , onParseError: function(){
                    this.css("backgroundColor", "#cc0000")
                }
                // if the error has been cleared, reset the bgcolor
                , onParseClear: function (){
                    this.css("backgroundColor", "");
                }
            });

            // automatically update the "#minNumber" field every time
            // the values are changes via the keyup event
            $("input[name^=min]").min("keyup", "#numberMin");

            // automatically update the "#minNumber" field every time
            // the values are changes via the keyup event
            $("input[name^=max]").max("keyup", {
                selector: "#numberMax"
                , oncalc: function (value, options){
                    // you can use this to format the value
                    $(options.selector).val(value);
                }
            });

            // this calculates the sum for some text nodes
            $("#idTotalTextSum").click(
                function (){
                    // get the sum of the elements
                    var sum = $(".textSum").sum();

                    // update the total
                    $("#totalTextSum").text("$" + sum.toString());
                }
            );

            // this calculates the average for some text nodes
            $("#idTotalTextAvg").click(
                function (){
                    // get the average of the elements
                    var avg = $(".textAvg").avg();

                    // update the total
                    $("#totalTextAvg").text(avg.toString());
                }
            );
        }
    );

    function recalc(){
        $("[id^=total_item]").calc(
            // the equation to use for the calculation
            "qty * price",
            // define the variables used in the equation, these can be a jQuery object
            {
                qty: $("input[name^=qty_item_]"),
                price: $("[id^=price_item_]")
            },
            // define the formatting callback, the results of the calculation are passed to this function
            function (s){
                // return the number as a dollar amount
                return "$" + s.toFixed(2);
            },
            // define the finish callback, this runs after the calculation has been complete
            function ($this){
                // sum the total of the $("[id^=total_item]") selector
                var sum = $this.sum();


                $("#grandTotal").text(
                //$("#grandTotal").text(
                //$("input[name^=grandTotal]").text(

                    // round the results to 2 digits
                    "$" + sum.toFixed(2)

                );
            }


        );

    }

    function test()
    {
        //Stores the grandtotal to var g_total
        var g_total = $("#grandTotal").text();    




        //Reforats..   $496.00  -->  496
        // Makes it workable
        var myArray = g_total.split('$');
        var myNewArray = myArray[1].split('.');


        //Submits the value to the #this works on line 391
        $('#thisworks').val(myNewArray[0]);

    }

    </script>
4

1 回答 1

1

var myNewArray = myArray[1].split('.');将创建一个这样的数组:

myArray = ["19", "99"];

如果您希望它返回 19.99,您可以更改:

$('#thisworks').val(myNewArray[0]);

至:

$('#thisworks').val(myNewArray.join('.'));

或者您可以在拆分之前使用该值:

$('#thisworks').val(myArray[1]);

我不确定这些是否是最好的解决方案,但它应该可以工作。

于 2013-03-08T22:31:34.903 回答