和中逗号的位置不正确
Incorrect Placement of Comma in Sum
我有一个表格,每行有一个文本输入。用户可以在每个文本框中指定一个金额。我的代码循环遍历每个文本输入并简单地对值求和。我的问题是,当用户输入超过>= 1,000,000的值时,总和变得不正确。例如,当用户输入1,000,000时,总和为1,000。
function init_icheck() {
$('#datatable input[type=checkbox]').iCheck({
checkboxClass: 'icheckbox_square-blue',
increaseArea: '10%'
});
}
// When Pay in Full Checkbox is Checked fill in Pay This Time Field with Invoice Amount Due Value
function paynow() {
var payFull = $('input[type="checkbox"].payfull');
payFull.on('ifChecked', function(event) {
$(this).parents('tr').find('.paynow').val($(this).val().replace('$', ''));
CalcFooter();
});
}
// If Pay in Full Unchecked then remove value from respective Pay This Time Input
// Only bind the ifUnchecked event if the checkbox is checked
function remove_checkbox() {
var payFull = $('input[type="checkbox"].payfull');
payFull.on('ifUnchecked', function(event) {
if ($(this).parents('tr').find('.paynow').val() == $(this).val().replace('$', '')) {
$(this).parents('tr').find('.paynow').val('');
CalcFooter();
}
});
}
// If Pay This Time changes recalculate total
function recalc_total() {
$('.paynow').keyup(function() {
var $ThisCheck = $(this).parents('tr').find('.payfull');
// Add Commas if # is over 1,000
$(this).val(addCommas($(this).val().replace(/,/g, '')));
if ($(this).val() == $ThisCheck.val().replace('$', '')) {
$ThisCheck.iCheck('check');
} else {
$ThisCheck.iCheck('uncheck');
}
CalcFooter();
});
}
// Recalc Function
function CalcFooter() {
var amtPage = 0;
var amtTotal = 0;
var Sum = 0;
$('.paynow').each(function(index, Obj) {
var value = parseFloat($(this).val().replace(',', ''));
if (!isNaN(value)) amtPage += value;
});
$('#datatable').DataTable().$('.paynow').each(function(index, Obj) {
var value = parseFloat($(this).val().replace(',', ''));
if (!isNaN(value)) amtTotal += value;
});
$('#amounttopay').text(
'Page: $' + addCommas(amtPage.toFixed(2)) +
' / Total: $' + addCommas(amtTotal.toFixed(2))
);
}
// Add Commas if value > 1,000
addCommas = function(input) {
// If the regex doesn't match, `replace` returns the string unmodified
return (input.toString()).replace(
// Each parentheses group (or 'capture') in this regex becomes an argument
// to the function; in this case, every argument after 'match'
/^([-+]?)(0?)('d+)(.?)('d+)$/g,
function(match, sign, zeros, before, decimal, after) {
// Less obtrusive than adding 'reverse' method on all strings
var reverseString = function(string) {
return string.split('').reverse().join('');
};
// Insert commas every three characters from the right
var insertCommas = function(string) {
// Reverse, because it's easier to do things from the left
var reversed = reverseString(string);
// Add commas every three characters
var reversedWithCommas = reversed.match(/.{1,3}/g).join(',');
// Reverse again (back to normal)
return reverseString(reversedWithCommas);
};
// If there was no decimal, the last capture grabs the final digit, so
// we have to put it back together with the 'before' substring
return sign + (decimal ? insertCommas(before) + decimal + after : insertCommas(before + after));
}
);
};
// Reinitialize iCheck on Pagination Change
$('#datatable').on('draw.dt', function() {
init_icheck();
paynow();
recalc_total();
remove_checkbox();
CalcFooter();
});
// Initialize Datatables
$('#datatable').dataTable({
"stateSave": true,
"oLanguage": {
"sSearch": "Search Results:"
}
});
我有一个简单的示例来说明这个问题。我提前感谢你为我指出了正确的方向。
http://jsfiddle.net/tgf59ezr/14/使用.replace(',', '')
只替换搜索字符串的第一个实例,导致数字解析不能按预期工作。
用这个代替:
.replace(/,/g, '')
g
表示替换所有实例。
String.prototype.replace()
相关文章:
- DIV并排,位置不正确
- 将CSS应用于在JavaScript中创建的表-表的位置不正确
- 谷歌地图标记IE中的位置不正确,在chrome上工作正常
- 按钮在 Chrome 中的位置不正确
- 动画过程中位置不正确
- 从 SVG 加载的结构 JS 形状的位置不正确
- 使用 jQuery 滑块缩放图像的位置不正确
- 为什么这些列表项的位置不正确
- Phonegap:在目录中导航,索引位置不正确
- Jquery日期选择器图标位置不正确
- 使用jQuery Mobile构建的iOS应用程序的页脚位置不正确
- 使用VS Code、TypeScript和Node.js的断点位置不正确
- 道场堆叠条形/柱形图表标签位置不正确
- 打印时绝对定位元素的位置不正确
- Html5画布绘制位置不正确
- 刷新页面后图像位置不正确
- 和中逗号的位置不正确
- 气球在地图上的位置不正确
- ng重复会导致锚点位置不正确
- jQueryUI可排序项目在拖动时位置不正确