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

Rounding off to 2 decimals

Community Beginner ,
Jan 13, 2022 Jan 13, 2022

Hi,

Could someone help me on how to modify this code so that the result would have 2 decimal places. tried changing the last part to: event.value="Php "+util.printf("%,0.2f", event.value), but didn't work.

Here's the script I used in custom format.

var x = event.value;
var n = x.toString().split(".");
n[0] = n[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
if(event.value == 0) event.value = "";
else event.value = "Php "+ n.join(".");

 

Hope someone could help. Thank you.

 

kmbt345_0-1642122251943.png

 

 

TOPICS
Create PDFs , General troubleshooting , How to , JavaScript , PDF forms
1.4K
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
1 ACCEPTED SOLUTION
Community Beginner ,
Jan 13, 2022 Jan 13, 2022

Hello,

Already found the solution which worked for me. I removed the other code, which maybe the reason for util.printf not working.

Here's the code:

if(event.value == 0) event.value = "";
else event.value="Php "+util.printf("%,0.2f", event.value);

 

Thank you.

View solution in original post

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 ,
Jan 13, 2022 Jan 13, 2022

How exactly is the util.printf code not working?  There is nothing wrong with it.

did you look in the console window for reported errors?

 

The other code that doe the split does not round. So it is incorrect for your stated purpose. 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 ,
Jan 13, 2022 Jan 13, 2022

Hello,

Already found the solution which worked for me. I removed the other code, which maybe the reason for util.printf not working.

Here's the code:

if(event.value == 0) event.value = "";
else event.value="Php "+util.printf("%,0.2f", event.value);

 

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
Community Expert ,
Jan 14, 2022 Jan 14, 2022

Well of course, you can't have to scripts competing for the same task. 

That's an important programming lesson. 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
New Here ,
Sep 16, 2022 Sep 16, 2022

How can I get this formual to round to only 2 decimals? The formula works perfectly for what I need except I am getting this as my result: 5.208333333333334 and I want it to read 5.21

 

var v1 = Number(this.getField("Current Salary").valueAsString);
var v2 = Number(this.getField("New Salary").valueAsString);
if (v2==0) event.value = "";
else event.value = (((v2-v1)/v1)*100.00);

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
New Here ,
Sep 16, 2022 Sep 16, 2022
LATEST

Never mind - I was actaully able to figure it out 🙂

 

var v1 = Number(this.getField("Current Salary").valueAsString);
var v2 = Number(this.getField("New Salary").valueAsString);
if (v2==0) event.value = "";
else event.value = (((v2-v1)/v1)*100.00); event.value=util.printf("%,0.2f", event.value);

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