February 11 2009 • 10:28 AM

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.

48 Responses discussing this post. Add yours below.

  1. Michael Trout
    February 11th, 2009 • 11:15 am • Link

    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.

  2. Eugene
    February 11th, 2009 • 11:59 am • Link

    Oh, very nice indeed. Thanks Steve, and InDS for sharing.

  3. Chris V.
    February 11th, 2009 • 12:27 pm • Link

    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?

  4. Chris V.
    February 11th, 2009 • 12:40 pm • Link

    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″

  5. Adi Ravid
    February 11th, 2009 • 2:31 pm • Link

    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.

  6. February 11th, 2009 • 2:35 pm • Link

    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.

  7. February 11th, 2009 • 2:39 pm • Link

    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!

  8. Steve Werner
    February 11th, 2009 • 2:41 pm • Link

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

  9. February 11th, 2009 • 7:17 pm • Link

    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.

  10. David Blatner
    February 11th, 2009 • 10:17 pm • Link

    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.

  11. Nadya Miloserdova
    February 12th, 2009 • 12:56 am • Link

    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

  12. walter Bravenboer
    February 12th, 2009 • 1:30 am • Link

    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

  13. February 12th, 2009 • 8:08 am • Link

    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.

  14. David Blatner
    February 12th, 2009 • 8:45 am • Link

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

  15. Nadya Miloserdova
    February 13th, 2009 • 12:04 am • Link

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

  16. Uwe Laubender
    February 13th, 2009 • 4:38 am • Link

    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.

  17. February 13th, 2009 • 7:48 am • Link

    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.

  18. David Blatner
    February 13th, 2009 • 9:43 am • Link

    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?

  19. David Blatner
    February 13th, 2009 • 9:44 am • Link

    Oops. @Nadya above just gave the same solution! Sorry. I was reading the comments backward. ;) Thanks, Nadya!

  20. Chris V.
    February 13th, 2009 • 3:20 pm • Link

    Thanks for the updated version. It works great!

  21. Nadya Miloserdova
    February 16th, 2009 • 4:27 am • Link

    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!

  22. February 17th, 2009 • 6:21 am • Link

    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,.]*”

  23. February 17th, 2009 • 8:38 am • Link

    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.

  24. February 22nd, 2009 • 6:33 pm • Link

    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

  25. David Blatner
    February 22nd, 2009 • 10:09 pm • Link

    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!

  26. February 23rd, 2009 • 7:19 am • Link

    Excellent, Steve … thanks!

    Have you added a Paypal “Donate” button yet? Let us know when you do.

  27. Nadya Miloserdova
    February 25th, 2009 • 3:54 am • Link

    Thanks a lot, Steve. This is fantastic!

  28. March 7th, 2009 • 5:26 pm • Link

    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

  29. March 11th, 2009 • 6:07 pm • Link

    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.

  30. March 12th, 2009 • 6:42 am • Link

    Thanks Steve!

  31. March 14th, 2009 • 4:15 pm • Link

    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!

  32. March 16th, 2009 • 9:25 am • Link

    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.

  33. David Blatner
    March 16th, 2009 • 11:14 am • Link

    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!

  34. Nadya Miloserdova
    March 17th, 2009 • 5:20 am • Link

    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!

  35. March 17th, 2009 • 9:36 am • Link

    Thanks David.

  36. David Blatner
    March 17th, 2009 • 4:33 pm • Link

    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!

  37. taggart
    March 18th, 2009 • 3:58 am • Link

    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

  38. March 19th, 2009 • 8:18 pm • Link

    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.

  39. taggart
    March 20th, 2009 • 2:50 am • Link

    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?

  40. Patrick
    March 25th, 2009 • 6:02 am • Link

    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.

  41. March 26th, 2009 • 8:55 am • Link

    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 :(

  42. March 26th, 2009 • 7:25 pm • Link

    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.

  43. March 27th, 2009 • 1:40 am • Link

    Thanks so muck.
    That function very good.

    Thank you again.
    :)

  44. May 4th, 2009 • 4:42 am • Link

    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

  45. Nadya Miloserdova
    May 4th, 2009 • 10:16 pm • Link

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

  46. Jongware
    July 20th, 2009 • 4:04 pm • Link

    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.

  47. December 1st, 2009 • 3:13 am • Link

    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?…

  48. Cathy Nelson
    February 25th, 2010 • 1:32 pm • Link

    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?

Subscribe to the Discussion

Get the ongoing discussion surrounding "Free Script to Change All Numbers in a Document using Math" delivered to you. Click here to subscribe via RSS.

Leave a Reply

You can use limited HTML tags, such as <em></em> for emphasis/italics and <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .

InDesignSecrets reserves the right to edit and/or remove posts and comments.