- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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>
最佳答案
var myNewArray = myArray[1].split('.');
将创建一个如下所示的数组:
myArray = ["19", "99"];
如果你希望它返回 19.99,你可以更改:
$('#thisworks').val(myNewArray[0]);
至:
$('#thisworks').val(myNewArray.join('.'));
或者您可以在拆分之前使用该值:
$('#thisworks').val(myArray[1]);
我不确定这些是否是最好的解决方案,但它应该可以发挥作用。
关于jquery - 删除 "$"+ sum.toFixed(2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15304314/
像下面这样使用 toFixed 得到: var a=0.5, b=1, c=1.5; console.log(a.toFixed(), b.toFixed(), c.toFixed()); // 0.
这个问题已经有答案了: Javascript toFixed function (6 个回答) 已关闭 9 年前。 当非小数部分大于 4 时,小数部分将被 chop 为 .3但当它小于 4 时,它会四
var number = 1.2; var rounded = number.tofixed() + 2 我正在尝试用任何数字求和 tofixed 值,但使用上面的代码我被四舍五入 = 12 吓到了.
我想知道之间的主要区别是什么 (2.3444).toFixed(2) ==> 2.34 还有 +(2.3444).toFixed(2) ==> 2.34 它们都给出相同的结果。谁能解释一下我什么时候需
根据这个post , 42..toFixed( 3 ); // "42.000" 上述代码有效,42..toFixed(3) 有效,因为第一个 . 是数字的一部分,第二个 . > 是属性运算符。
在许多情况下,toFixed() 会因 JavaScript 数学中的浮点而失败。 我找到了这个解决方案: function toFixed(decimalPlaces) { var factor =
我有以下代码块: export interface Record { Percentage_Emissions: number; Percentage_Emissions_Previous: numb
我正在使用 javascript 绑定(bind)到一些复选框,toFixed(2) 没有四舍五入。任何想法为什么它不四舍五入?例如,如果号码是 859.385,它只会显示 859.38 而不是 85
在我的程序中,我需要四舍五入到最接近的两位小数的“浮点”数字,经过一番研究,我决定使用 toFixed(..) 来实现此目的,如下例所示。使用 toFixed() 的缺点是什么?它在所有浏览器中都能正
美好的一天, 我仍在学习 JS 和 HTML,我注意到一些对我来说非常有趣的事情 我正在使用 Google 地理编码脚本,并在 map 上创建了一个点击事件来检索 GPS 坐标, function
我正在使用 toFixed 但该方法未按预期运行 parseFloat(19373.315).toFixed(2); //19373.31 Chrome 预期输出:19373.32 parseFl
我正在测试 javascript 的 toFixed() 方法。结果如下图。 (49.175).toFixed(2) => "49.17" (49.775).toFixed(2) => "49.77"
在特定组的用户的帐户仪表板上会显示一个唯一的编号。该数字是从数据库中获取并经常更改的。数量小于 10,000,000。 如果元素的宽度开始 chop 文本,那么将数字从“2,643,977”更改为“2
我的代码出现错误,以前从未遇到过这个错误,这真的很奇怪。我确实尝试过 parseFloat 但这也不起作用..代码:https://gist.github.com/markd69/aca03cab20
我正在使用 KnockoutJS,但遇到了一个问题,我不知道如何寻找解决方案: 我有一个包含记录的表: Edit Enter Hour
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
对于四舍五入小数(价格),我已经使用 .toFixed(2) 一段时间了。但我最近发现 JavaScript 不能“精确”四舍五入小数。我有点震惊,即使 10.005 也无法正确舍入为 10.01。它
我有以下代码来计算并显示两个值的总和。 var oldprice_formated = parseFloat(oldprice).toFixed(2); var extraPrice = parseF
这是我的脚本: jQuery(document).ready(function () { jQuery('#btnCalculate').click(function () {
我想保留数字后面的两位数字,即 2.89 或 2.00。谷歌把我带到this answer使用.toFixed(2)。 虽然效果很好,但在输入 input 值时效果不佳: const [ value,
我是一名优秀的程序员,十分优秀!