Rounding off to 2 decimals

Community Beginner ,
Jan 13, 2022 Jan 13, 2022

Copy link to clipboard

Copied

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
Comment review and collaborate PDF , Create PDFs , General troubleshooting , How to , JavaScript , PDF forms

Views

110

Likes

Translate

Translate

Report

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 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.

Likes

Translate

Translate
Community Expert ,
Jan 13, 2022 Jan 13, 2022

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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);

Likes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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);

Likes

Translate

Translate

Report

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