Free Script to Change All Numbers in a Document using Math
What if you had a document with 2,000 prices in it, all in US dollars, and you wanted to change them all to Euros? Taking a look at today’s exchange rate, we see that the dollar is around 1.3 Euros. It’s easy to do a find/change to replace the $ symbol with the €. But what about all those numbers? Wouldn’t it be cool if we had a script that simply multiplied all the numbers in the document by 1.3? Too bad that’s impossible.
Until now!
Scripting pro Steve Wareham just sent me a script that lets you multiply, divide, add, or subtract the numbers in your document. It’s called NumberAdjuster, and we’re releasing it free here, as promoware. (In other words, if you like it, go hire him to do some scripting the next time you need something automated.)
Here’s how it works: You download the script, unzip it, install it, then double-click it in the Scripts panel (Window > Automation > Scripts). You should see a dialog box like this:

You can control whether the operation is performed on the entire document or just the current selection. You can specify which math operation to perform and what to use as the operator (such as “multiply by 1.3″ or “add .99″). You can constrain the operation to numbers with a prefix (such as only numbers with a $ or £, or even only apply it to negative numbers by typing a dash). And you can constrain the operation to numbers with a particular character style.
The first time you run it, it might be slow, because it has to write a little preferences file to disk (so it remembers what you did next time you run the script). After that, it’s pretty dang fast.
Don’t type in two prefix characters (such as -$ for negative dollars). It can’t handle that, and the result will require an undo. In fact, as with running any script, I strongly suggest saving your document first, in case you need to choose file > revert. (You can undo the math operations, but it undoes them one number at a time, which isn’t much fun if you’ve just changed a thousand numbers.)
Note that this script won’t change the prefix for you (changing dollars to euros, or whatever). It only adjusts the number. But again, changing the symbol is as easy as Edit > Find/Change.
I was just chatting with Anne-Marie about this, and she told me she heard of a catalog company that put a “take 15% off all the prices on this page” sticker on every page of a big catalog because it was easier and faster than going ahead and changing the prices. No longer! This script is a marvel.
Nice.
A basic usability enhancement would be to allow the script to work on the story level, too. Document and Selection level edits have their place, but Story level would be beneficial.
Oh, very nice indeed. Thanks Steve, and InDS for sharing.
Hmmm, I did a test to see how this worked. Using CS3, I created a new document with the following in a text box:
test
$5
505
L505
$3.09
I ran the script using multiply by 3, prefix of $. The end result was:
test
$15
505
L505
$99
Now, if I do “3.03″ in its own text box, it gives the correct answer of 9.09.
Is it just me or CS3 doesn’t play nice with it?
Sorry for the double post.
Oh, and that was using “selection.” If I choose entire document, it seems to work fine.
And above, I meant “3.09″ gave “9.27″
And the 2009 Perfect Timing Award goes to… David Blatner… ta da da da (background music)
Tell me the truth, did you here me pray? Did you feel my suffering?
This is just what I needed and was just about to seat down and write it.
All I need to do is add rounding options to the results and I’m there, totally out there.
I truly love you man.
I tried it in CS3 using the character style option. It changed all numbers regardless of character style. Maybe I didn’t select or set something properly.
OK, shows me for not checking before I opened my mouth. Somehow the chapter numbers had a character style of “Verse Numbers+”. I fixed it and script works great.
I will always double check before I write a negative comment!
>Tell me the truth, did you here me pray? Did you feel my suffering?
That’s why David is called the InDesign God.
He responds to your calls!
Thanks for finding that bug, Chris. That would happen when you search on a selection and there were no other characters after the last number found.
I’ve made a change the regular expression used and sent it over to David.
LOL! I’m glad the script is so useful to folks. I love it.
I have uploaded Steve’s new version (it’s labeled 1b after unzipping). Same link as above. He also wrote: “I’ve updated the script so that it will round numbers to two places after the decimal point.” That’s a big help, too.
This script is wonderful. It works fine.
I even managed to find the line where I can make changes if I need to adjust the accuracy of the result (i.e. amount of digits after decimal point). It is the line 263 (in Extend Script editor).
You have to change both 100s in line 263 into whatever you want – 1000s for .accuracy of 0.001, into 1s for integer digits.
If the option of accuracy was in menu, some people may appreciate that.
Me – definitely.
Please inform me if any updates for this outstanding script happen to appear.
Nadya
Moscow
Any idea if this works with international numbers? We use a comma where the U.S. use a point,
for instance ‘one thousand’ is written as 1.000
and ‘one euro and 30 cents’ is written as 1,30
It is completely awesome that Steve Wareham is releasing this script for free.
He’s on my short list of ID scripting pros that I pull from to refer clients to when they really really need something in ID that it doesn’t offer.
David linked to his site in the post, but let me spell it out for you: http://www.stevewareham.com. Lots of other scripts there! And his contact info!
Steve, though, what you’re missing on your site is a large Donate button so users can show their appreciation for your hard work.
I know I was looking for one.
@Walter: I’ll check with Steve about international numbers. Actually, I just noticed that this current script does not work well at all on numbers that have commas in them. Oops! I’ll go tell Steve.
I’m noticing a few extra bugs in this version. For example, I can’t get it to work on a selection anymore. Hm.
Well, it’s a work in progress.
@Walter: I would temporarily replace al the decimal commas into dots for the purpose of this script; let it do the job, and then replace dots to original commas .
Thank you Steve (@David) for publishing this.
I have a real complex index which was heaviliy edited by the customer. He did a little shuffling of items which were in the wrong order. Among others there were roman numerals (e.g. IX was put just after IV), so it was a nightmare for me hearing that he’d like to insert a few extra pages at the beginning of the book. Meaning all page numbers listed in the index had to be changed!
With “Number Adjuster” no problem at all.
Of course it would be fine to find a solution for doing math with numbers containing commas.
I’ve sent David a updated script that can work with numbers with commas in it. Perhaps the original name for this script should have been “small number adjuster” as it just worked with numbers up to 999. Or larger numbers if they were comma free
The latest version should work with all numbers, no mater how large or how many commas they contain. Creating an international version is possible, but I may not get to that for a while.
Sorry for the delay; version 1c is now up there (same link as the one in the blog).
I agree that it’d be cool if it could handle European commas/decimals, but one idea might be to use a grep find/change to convert the dots to commas and vice versa, then run the script, then convert the commas/dots back. Would that work?
Oops. @Nadya above just gave the same solution! Sorry. I was reading the comments backward.
Thanks, Nadya!
Thanks for the updated version. It works great!
Oops! In version 1c my Russian decimal commas act like a dope to this script!
Compare:
a) if I have an value of 1.3 and ask the script to multiply it by 10, I get 13. This seems correct.
b) If I take 1,3 ten times, the script yields me even 130! I wish I could possibly get this kind of profit in my life!
for InDesign CS4
This text is not match when running InDesign CS4
“foo123var”
a cause:
“-*[0-9][0-9,.]*\\b”
This regex is not work well in InDesign CS4
Use this regex for CS4:
“-?[0-9][0-9,.]*”
Nadya, yes the script will only work properly on numbers formatted as “1,234.56″ and not as “1.234,56″.
Seuzo, I’m not sure if that’s a problem with CS4, the script was intended not to find numbers that are part of words, so I believe you would get the same results with CS3.
I’ve sent David version 1.1 of this script. New features are outlined below.
1) User can specify the format numbers are in for searching and replacing. Either 1,234.56 or 1.234,56 for the countries that use that style of formatting. The operator, however, still needs to be entered in the format 1,234.56.
2) User can specify how many places to round the result to.
3) User can choose to search on the parent story of the currently selected text frame.
4) User can enter a custom regular expression to use in the search
Wow, thanks, Steve! I have updated the link in the blog post to the new “1dot1″ version. I tested only for 2 minutes, but it looks pretty darn cool!
Excellent, Steve … thanks!
Have you added a Paypal “Donate” button yet? Let us know when you do.
Thanks a lot, Steve. This is fantastic!
I’m noticing that the script, when using it to find numbers with a $, only converts the firsts number in a line.
I have a paragraph of copy that mentions two different dollar amounts, and the script only converts the first one.
Same problem for instances where I have a few item numbers and prices on the same line separated by tabs.
..
Even with these few occurrences, this script saved me sooooo much time, and I am very thankful!
-Sean
Thanks Sean. I’ve sent David an updated script that addresses the bug you mention. It’s also been updated to work with custom regular expressions that use lookarounds.
Thanks Steve!
So helpful, Steve! I have chapters in a book that have reference numbers in text that have to be renumbered, and this has really helped. Yes, I still do have to visually check to catch the ones that have a span with en dash, or a comma between two or more numbers, but these are a small minority. The important thing is that this saves a lot of physical stress!
How can we tell if the latest script is linked in the blog? It looks like the old one I think, but I can’t really tell.
My apologies for the delay. I have uploaded version 1.2 (“number_adjuster_1_2.zip”) and updated the download link above. Thank you, Steve!
Dear Steve,
You did the great work. Is there anything a person could wish for?
Afraid, so.
I’d be happy if I could have the following options for formats:
Numbers are formatted as:
1,234.56
1.234,56
123.456
123,456
These options would probably cover all the possible combinations.
Anyway, thank you from the bottom of my heart!
Thanks David.
By the way, it’s important to save before running a script like this, and then proof the result carefully. This is a work in progress (and it’s free!) and InDesignSecrets and Steve do not guarantee the results!
As so many have said, it truly is a great time-saver. Thank you very much, Steve!
I was just playing around with it. In the latest version multiplying by 1.3 didn’t work for me (I have set it to round to 2 places). When I reopen the Number Adjuster it has shortened my “1.3” to “1”, which is indeed what has been calculated.
Any thoughts?
Regards Taggart
Sorry Taggart, I haven’t been able to reproduce your results. Perhaps something happened to the preferences file. Your could try to delete it and run the script again. The preferences file is located in the same folder that you installed the script and is named “num_adjust_prefs.txt”.
I don’t think that would cause the problem, but it’s all I can think of right now. I will post an update if I find something that causes this.
Thanks for your reply, Steve!
Deleting the preferences file didn’t help. I’m using InDesign CS 3 5.0.4 on OS X 10.5.6. Maybe someone else with this setup has a similar problem?
Hey Steve,
Your Script is almost perfect now. What would be cool is, that it could round the decimals in 5er steps (i.e. 10.56 to 10.55 and 10.48 to 10.50). Is that possible?Thanks again.
Hello,
I’m French.
I would like use the great script.
And me, i use, for sample : 2530,80 €
Someone could adapte the script for use the sign € on suffixe ?
Please
Michael,
If you have the current version of the script (version 1.2) then I believe you can do this if you use a lookaround in a custom regular expression.
So to find this: “2530,80 € ” you could use a regular expression such as “[-]*[0-9][0-9,.]*(?= €)”.
I’m not sure if the results will be formatted exactly as you would like them though. Currently the script supports the two styles “1,234.56″ and “1.234,56″ but not “1234,56″ where there is only a comma and no period.
Also, if your result is something like “2530,80″ then the script might return “2530,8″. This is something that will probably be addressed in the next update.
Thanks so muck.
That function very good.
Thank you again.
Is there a way to build a Math.round() method into the script…? With the option for the rounding values: 1,234 => 1,230/1,23/1,2/1
@Martin:
You may use this script: Choose Multiplication by 1 and specify how many places you want it to round to.
Unfortunately, you can’t obtain 0 at the end of the final result.
Ok, maybe this simple GREP query can help.
Assume that you want get 1,234 => 1,230.
First run the script. Round all your nubers up to 2 places after the comma. You’ll have 1,23 this time.
Then run Find/Change.
Find What (\d\.\d\d)
Change to $00 (that means the found expression followed be zero.
In case it isn’t fixed as yet, trailing zeroes can be preserved as follows.
Do not divide by 100; or, if the number already has 2 decimals, multiply it by 100.
Do your calculations, rounding off to integers at will, and only when writing out the number, insert the decimal period at its correct place in the string (not as a numerical value).
This way, a value of “25.10″ will have its calculations done upon as “2510″ — such as multiplying by 2, making it “5020″. On inserting the number back into the document, add the period 2 chars from the right and write “50.20″.
This long discussion — http://forums.adobe.com/message/2082462#2082462 — walks a beginning scripter through the whole process, albeit with some sidetracks.
Nice scripting, but if whe’re talking about changing a pricelist, there’s a huge gap
You simply can’t multiple a price by 1.03 (index adaptation).
Does somebody has a solution for this error?…
I love the script. However, my customer wanta all numbers rounded UP to the next dollar. Example: $100.11 x 1.03 = $103.11, customer wants $104 not $103. Any ideas?