Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Calculation result separated in whole number and decimal, not rounding correctly

Community Beginner ,
May 26, 2016 May 26, 2016

I have 2 fields for the result of a division: the whole number and the decimal (1 decimal). I Math.floor the result to get the whole number and all goes well until I hit a case where the result is 92.97.

This should give "93" in the whole field and "0" in the decimal field, instead it gives me "92" and "10" (even though I only allow 1 character in the field).

How do I get the "93" and "0" in the following code?

fieldLVEDV = this.getField("LVEDV").value;

fieldBSA = this.getField("BSA").value;

if (fieldLVEDV != "" && fieldBSA != ""){

totalLVEDV = this.getField("LVEDV").value + ((this.getField("LVEDVDec").value)/10);

calculation = (totalLVEDV / fieldBSA);

calculationDecimal = calculation;

partWhole = Math.floor(calculation);

if(partWhole <0) {partWhole ="";}

this.getField("Index").value = partWhole ;

this.getField("IndexDecimal").value = Math.round(10*(calculationDecimal-partWhole));

{if (this.getField("IndexDecimal").value = "10")

{

this.getField("Index").value = partWhole+1;

this.getField("IndexDecimal").value = "0"

}}

} else {

    this.getField("Index").value = "";

    this.getField("IndexDecimal").value = ""; 

}

I thought adding the blue part would do the job but obviously it didn't work.

TOPICS
Acrobat SDK and JavaScript
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 26, 2016 May 26, 2016

Ah, your problem is that you are not using the correct comparison operator. To check if two values are equal, the operator is "==" an not not "=". By using "=", you are assigning the value 10 to the field, which you are then changing to 0 a couple of lines down. Use "==" and things should work correctly.

Translate
Community Expert ,
May 26, 2016 May 26, 2016

What data do you have in your input fields (LVEDV, LVEDVDec and BSA) for the problem to occur?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 26, 2016 May 26, 2016

LVEDV: 178

LVEDVDec: 5

BSA: 1.92

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 26, 2016 May 26, 2016

It looks like you are doing the right thing with adjusting the partWhole variable by one if your number of decimals is 10. It also gives me the correct output when I plug that into a simple form.

What application are you using when the result is incorrect?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 26, 2016 May 26, 2016

I get a result of "92" and "1"

I use Adobe Acrobat Pro 10. I put the code in the "On Blur" action of the LVEDV and LVEDVDec fields.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 26, 2016 May 26, 2016

It also works for me in Acrobat X. Are you sure that both scripts are the same? I would create a document level script and call that from the individual on-blur actions. Alternatively, you can also create a new hidden and read-only field, and use your script as the custom calculation script.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 26, 2016 May 26, 2016

Do you get 92.7 (178/1.92) before entering the 5 in the decimal field to make it 178.5?

If I remove the blue script, I would get 92.7 for 178 (correct answer), and 92.1 for 178.5

With the blue script, I get 93.0 for both 178 and 178.5.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 26, 2016 May 26, 2016

Ah, your problem is that you are not using the correct comparison operator. To check if two values are equal, the operator is "==" an not not "=". By using "=", you are assigning the value 10 to the field, which you are then changing to 0 a couple of lines down. Use "==" and things should work correctly.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 27, 2016 May 27, 2016
LATEST

That worked well. Thanks

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 26, 2016 May 26, 2016

I would use the util.printf() method to round the result of the division and then the modulo operator to get the decimal fraction.

fieldLVEDV = this.getField("LVEDV").value;
fieldBSA = this.getField("BSA").value;
if (fieldLVEDV != "" && fieldBSA != ""){
// perform the divison and roud the result;
// calculation = (totalLVEDV / fieldBSA) rounded to 1 decimal place;
totalLVEDV = util.printf("%,1 1.1f", fieldLVEDV / fieldBSA);
console.println(totalLVEDV);
// get the whole number protion of the result;
var partWhole = Math.floor(totalLVEDV);
var partDecimal = Math.floor((totalLVEDV % 1) * 10); // remainder when divided by 1;

console.println( partWhole + " " + partDecimal);
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines