Chrome在线PDF表单计算异常问题求助
Fixing Calculation Errors in Finnish PDF Travel Expense Forms for Chrome
I’ve dealt with this exact quirk before—Chrome’s built-in PDF handler doesn’t properly recognize comma-based decimal separators like Finnish uses, which is why your calculations work flawlessly in Acrobat DC but break in Chrome. Let’s break down the issues and fix them step by step:
Problem Breakdown
- Sum calculation fields (like the total including "Taksi"): Chrome parses comma-separated values like
20,50as the integer20050(it strips the comma entirely instead of treating it as a decimal separator), leading to that absurdly high total. - "Kilometrikorvaus" custom calculation: When you input
0,44, JavaScript (which Chrome uses for PDF form logic) can’t parse the comma as a decimal.parseFloat("0,44")returnsNaN, so multiplying by anything results inNaN—hence thenan €display.
Fix 1: Repair Sum Calculation Fields
Ditch the default "Sum of fields" setting (which Chrome mishandles) and use a custom JavaScript calculation that explicitly converts comma-separated strings to valid numbers:
- Open the PDF in Acrobat DC (you can’t edit form calculations in Chrome).
- Locate your total calculation field, open its Properties > Calculate tab.
- Select "Custom calculation script" and replace any existing code with this (update the
expenseFieldsarray to match your actual field IDs):// List all fields you need to sum (replace with your form's actual field names) var expenseFields = ["taksi", "hotelli", "ruoka", "km1_total"]; var total = 0; expenseFields.forEach(function(fieldName) { var fieldValue = this.getField(fieldName).valueAsString; // Convert comma-separated string to a JavaScript-compatible number var numericValue = parseFloat(fieldValue.replace(",", ".")) || 0; total += numericValue; }); // Convert total back to Finnish format with comma separator and euro symbol event.value = total.toFixed(2).replace(".", ",") + " €"; - Save the PDF and test in Chrome—inputting
20,50in "Taksi" should now show the correct total.
Fix 2: Fix Custom Kilometer Calculation
Your existing script doesn’t account for comma separators. Update the script for the "Kilometrikorvaus" calculation field to:
var kmField = this.getField("km1"); var rateField = this.getField("a5"); // Convert field values to valid numbers (handle Finnish comma separators) var kmValue = parseFloat(kmField.valueAsString.replace(",", ".")) || 0; var rateValue = parseFloat(rateField.valueAsString.replace(",", ".")) || 0; if (kmValue * rateValue !== 0) { var result = kmValue * rateValue; // Format result back to Finnish decimal format for display event.value = result.toFixed(2).replace(".", ","); } else { event.value = ""; }
Key Changes Explained:
valueAsStringensures we get the raw input string (e.g.,"0,44"instead of a corrupted numeric value).replace(",", ".")converts the string to a format JavaScript can parse as a decimal.|| 0handles empty fields or invalid inputs, preventingNaNerrors.toFixed(2).replace(".", ",")converts the calculated number back to the Finnish comma-separated format for proper display.
Testing Tips
After making changes, verify these scenarios in Chrome:
- Input
20,50in "Taksi" → total should show20,50 €(if it’s the only expense). - Input
2in the km field and0,44in the rate field → calculation should show0,88 €. - Test empty fields to ensure they don’t break the total (should default to 0).
内容的提问来源于stack exchange,提问作者Squrppi
相关产品推荐
相关产品推荐

