Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

# Javascript math in a fillable PDF

4 replies to this topic

### #1 mysticduck

mysticduck

• Members
• 35 posts
• OFFLINE
•
• Gender:Male
• Location:Southern Oregon
• Local time:05:25 AM

Posted 23 January 2014 - 08:27 PM

Hello all.

I'm working on a fillable PDF and am needing to do some maths in it.  I need to take the difference of field A and B then divide the answer into field B as a percent then put the answer in field C.

So for example

A=32400 B=38200

A-B=400

400/B% or 400/38200%

C=1.22 (rounded up)

I do realize this would be MUCH easier in Excel, but I'm trying to make this work with the PDF I already have.  I'm using NitroPDF as an editor, and it gives me the option to use a simplified field notation or a custom calculation script.  If anyone could point me in the right direction I would much appreciate it.

Stultorum infinitus est numerus

### #2 groovicus

groovicus

• Security Colleague
• 9,963 posts
• OFFLINE
•
• Gender:Male
• Location:Centerville, SD
• Local time:06:25 AM

Posted 24 January 2014 - 07:00 AM

So you need to figure out how to do this in javascript?

### #3 mysticduck

mysticduck
• Topic Starter

• Members
• 35 posts
• OFFLINE
•
• Gender:Male
• Location:Southern Oregon
• Local time:05:25 AM

Posted 24 January 2014 - 12:05 PM

Correct.  When I choose to edit either the simplified notation option or the custom calculation script it opens a new window with the heading "Enter the JavaScript you would like to use below."

Stultorum infinitus est numerus

### #4 groovicus

groovicus

• Security Colleague
• 9,963 posts
• OFFLINE
•
• Gender:Male
• Location:Centerville, SD
• Local time:06:25 AM

Posted 24 January 2014 - 04:24 PM

Off the top of my head:

```function format(a,b){
var c = a-b,
d = c/b;

return d.toFixed(2);
}
```

You should use a better function name than I did, and you should use meaningful names for the variables so it makes sense for the next person, but I think that ought to do it. Also, you say that you want to round up. Do you mean that you want the number formatted to two decimal places, following conventional rounding rules, or do you want the ceiling? It looks like you just want the number formatted.

Edited by groovicus, 24 January 2014 - 04:25 PM.

### #5 mysticduck

mysticduck
• Topic Starter

• Members
• 35 posts
• OFFLINE
•
• Gender:Male
• Location:Southern Oregon
• Local time:05:25 AM

Posted 24 January 2014 - 05:26 PM

Formatted to two decimals rounded conventionally.  Thanks for the code, will try it out over the weekend.

Stultorum infinitus est numerus

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users