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

Stop Automatic Calculation Rounding or set None format category to two decimals?

Explorer ,
Oct 31, 2016 Oct 31, 2016

Hi, how do you stop automatic rounding in a calculation result?  The results field needs to display the actual unrounded number with two decimal points (it is a currency), and I have a simple multiplication calculation script:

var v1 = this.getField("Construction").value;

event.value = v1 * .001;

if ((event.value == 0) && (getField("Construction").valueAsString === "")) event.value = "";

When the format category is set as Number with two decimal places, the result is automatically rounded.  When the format category is set as None, the result is unrounded but there are more than two decimal places.  What should be done to display the actual unrounded number with a maximum of two decimal places?

TOPICS
Acrobat SDK and JavaScript
2.7K
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 , Nov 01, 2016 Nov 01, 2016

Use this code:

event.value = Math.floor(v1/10)/100;

Translate
Community Expert ,
Oct 31, 2016 Oct 31, 2016

Change this line:

event.value = v1 * .001;

To:

event.value = (v1 * .001).toFixed(2);

And also change the Format option to None.

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 ,
Oct 31, 2016 Oct 31, 2016

It's not at all clear to me what you want. You say you want to limit it to two decimal places, but at the same time say that a number format limited to two decimal places isn't what you want. Can you clarify with the following example?:

Field value: 123456

Field value multiplied by 0.001: 123.456

This resulting value should then be displayed as:

A. 123.45

B. 123.46

C. 123.456

D. Something else: _________

Using the toFixed(2) method will give option B, which is what setting the number format to 2 decimals will do.

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
Explorer ,
Nov 01, 2016 Nov 01, 2016

Thank you both, and you're right, I didn't fully explain.  We need option A as the result.  I've been googling to try to find the answer but have a feeling I'm not using the correct language when searching.  I've looked for "adobe pro display true value" and variations with  "javascript "unrounded value" "actual value" but there's probably a better term.  Any additional advice to get option A would be appreciated. 

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
Explorer ,
Nov 01, 2016 Nov 01, 2016

I tried the following but it didn't make a difference:

var v1 = this.getField("Construction").value.toFixed(2);

event.value = (v1 * .001).toFixed(2);

The Construction field is set up as a format category Number with two decimal places (for currency), I tried switching it to format category None and I added  + in front of this.getField but it didn't result in option A.

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 ,
Nov 01, 2016 Nov 01, 2016

Use this code:

event.value = Math.floor(v1/10)/100;

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
Explorer ,
Nov 01, 2016 Nov 01, 2016
LATEST

Thank you!!! 

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