is now part of CreativePro.com!

*** From the Archives ***

This article is from February 11, 2009, and is no longer current.

Free Script to Change All Numbers in a Document using Math

96

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). [See note at end of this article for a different version of this script.] 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.

[Update: Here is an updated version of the script, written by Peter Kahrel.]

David Blatner is the co-founder of the Creative Publishing Network, InDesign Magazine, CreativePro Magazine, and the author or co-author of 15 books, including Real World InDesign. His InDesign videos at LinkedIn Learning (Lynda.com) are among the most watched InDesign training in the world.
You can find more about David at 63p.com

Follow on LinkedIn here
  • Michael Trout says:

    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.

  • Eugene says:

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

  • Chris V. says:

    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?

  • Chris V. says:

    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”

  • Adi Ravid says:

    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.

  • Dave says:

    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.

  • Dave says:

    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!

  • Steve Werner says:

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

  • Nadya Miloserdova says:

    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

  • walter Bravenboer says:

    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

  • Anne-Marie says:

    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: https://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.

  • Nadya Miloserdova says:

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

  • Uwe Laubender says:

    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!

  • Chris V. says:

    Thanks for the updated version. It works great!

  • Nadya Miloserdova says:

    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!

  • seuzo says:

    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!

  • Anne-Marie says:

    Excellent, Steve … thanks!

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

  • Nadya Miloserdova says:

    Thanks a lot, Steve. This is fantastic!

  • Sean Larkin says:

    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.

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

  • Sean Larkin says:

    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!

  • Nadya Miloserdova says:

    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!

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

  • taggart says:

    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.

  • taggart says:

    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?

  • Patrick says:

    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.

  • Michael says:

    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.

  • Michael says:

    Thanks so muck.
    That function very good.

    Thank you again.
    :)

  • Martin says:

    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

  • Nadya Miloserdova says:

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

  • Jongware says:

    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 — https://forums.adobe.com/message/2082462#2082462 — walks a beginning scripter through the whole process, albeit with some sidetracks.

  • Bart says:

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

  • Cathy Nelson says:

    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?

  • Martin Fischer says:

    Hi Taggart,

    > When I reopen the Number Adjuster it has shortened
    > my ?1.3? to ?1?, which is indeed what has
    > been calculated.

    This is because your decimal point as a result of the realEditbox gets converted into a comma (because of your system preferences).

    Steve cannot reproduce it, because his decimal point comes as a point – as expected by JavaScript.

    Your ‘1.3’ is converted to ‘1,3’.
    parseFloat(myOperator) cuts ‘1’ and ‘,3’ gets lost.

    So change line 262 like this:
    var myOperator = myOperatorField.editContents.replace(‘,’, ‘.’);

    Best
    Martin

  • Martin Fischer says:

    @Jongware

    > In case it isn?t fixed as yet, trailing zeroes can be preserved as follows.

    Just replace the numRounder-function with the function below:

    function numRounder(myNumber, myRoundto)
    {
    myRoundMod = 1;
    for(rounding = 0; rounding < myRoundto; rounding++)
    {
    myRoundMod = myRoundMod * 10;
    }
    myRoundedNum = String(Math.round(myNumber*myRoundMod)/myRoundMod);
    var dec_index = myRoundedNum.indexOf('.');
    if (dec_index != -1)
    {
    var my_postComma = myRoundedNum.substring(dec_index +1,myRoundedNum.length);
    for (var i = 0; i < myRoundto – my_postComma.length; i++)
    myRoundedNum += '0';
    }
    return myRoundedNum;
    }

    Best wishes
    Martin

  • Jon Marken says:

    Wow, this works great. A client produced his own index and we had to add two pages to the beginning of the book. This made the changes very easy. Second time David Blatner saved my skin. Thanks!

  • carola says:

    have you any idea, how it could work with CS2?
    I´m not to sophisticated nor in my english nor in scripts – but i have to get it work …

    best wishes
    Carola

  • Mick says:

    I got a syntax error on this line

    for (var i = 0; i < myRoundto ? my_postComma.length; i++)

    Should be
    for (var i = 0; i < myRoundto – my_postComma.length; i++)

    It was fine for making 3.5 up to 3.50, but 3.00 still came out as 3 so I have added these lines

    if (dec_index != -2)
    {
    var my_postComma = myRoundedNum.substring(dec_index +1,myRoundedNum.length);
    for (var i = 0; i < myRoundto – my_postComma.length; i++)
    myRoundedNum += '.00';
    }

    Works okay now, but I'd like the "Only accept numbers with prefix …" nox to accept a tab , as well as a currrency symbol.
    Can this be done?

    • Jarvice says:

      I tried using your script but it doesn’t work for me. If i try 1,50 x 2 it placces 3 instead of 3.00. Any Tips?

  • JP says:

    Do you have this for Mac?

  • @JP: Not sure what you mean. Javascripts work on both mac and windows versions of InDesign.

  • Tara says:

    I install the script into CS3, but it won’t run and I desperately need this to work. What am I doing wrong?

    Thanks.

    Tara

  • hassen says:

    I installed your script in indesign cs4
    but your script does not work with the comma,
    thank you for finding a solution.
    Thanks.
    Hassen

  • top says:

    Thank you, it very comfortable!

  • Roberto says:

    Can anyone help me?
    I need to modify all the prices on the pricelist of my company, the problem is the format is either 1.200 (meaning 1200 ?) or 430 (430 ?)
    I need to add 4% to all the prices.
    I tried successfully to modifiy all the numbers *.*** using the regex system but only to add or multiply for natural numbers, I can’t multiply for 1,04 with this script.

    At the same time I need to mantain all the codes, for example 45.48.86 so I’d need to add an exception for this configuration of numbers, but not for the two digits one by themselves.

    In some places there are number like 100 that need to stay the same, usually the line is “O100” or “O 100” (it means the diameter is 100mm). If anyone can help with these, as it’ll take a life to modify all the prices manually!!

    1.000 need to become 1.040
    100->104
    O100 or O 100 stays the same
    34.56.32 stays the same
    When the number have decimals it need to round up for excess…

  • Kate says:

    Perfect script! Worked beautifully for updating a table of contents where the page numbers shifted a page because of edits. Thank you!

  • shorshe says:

    Hi !
    Script worked just great! Thanks!

    For me, it would be even greater to have a customizable GREP Search for specific number, if i may add a Feature Request :)

    Thanks again
    shorshe

  • Chuckie says:

    I tried a simple test:
    5 pm
    6 pm
    11:30
    11:45.

    My results with a subtraction of 1 were ?
    4 pm
    5 pm
    10:29
    10:44

    That was a rounding factor of 0. Other rounding factors gave unpredictable results. So either I can’t figure out the variables or it cant do times with minutes. I’ll keep trying, but the secret would lie in a help file because I cant make sense of the some of the variables in the script.

  • @Chuckie: Well, it’s not set up to specifically work with times. You’d have to probably do some tricky stuff either with grep, or with the prefix, or perhaps apply different character styles to different parts (like hours vs. minutes). I mean, after all, if you say “subtract 1” how is it supposed to know if you mean 1 minute or 1 hour?!

    • Alex B. says:

      I just tried running this in InDesign CC and unfortunately it only works with natural numbers. I need to multiply by 1.25, but the script automatically corrects it to 1.

      I even tried manually editing the prefs.txt file to 1.25, which correctly displays 1.25 in the script dialogue window, but as soon as it’s run, it defaults back to 1.

      Is there a work around here?

      Any help is much appreciated. This is an awesome script and exactly what I’m looking for. Provided it works.

      • Alessandro says:

        …same problem…
        I need to multiply by 1.3, but it doesn’t work :-(
        Is there an updated release of it?

        Thanks

    • Alessandro: That is strange… I have no problem with this working. (I’m currently on Mac OS 10.9.1, InDesign CC 9.2…)

      Be sure to set the “Perform Operation On” buttons to “Story of selected frame” or select the text first!

      • Henning Ryberg says:

        I have had this problem for an while now, I do an work around, first I multiply with eg. 28 then i divide with 10 this gives 2.8 :-)

  • Mark S says:

    This is a great script! I have been using for the past 3 years. This year my client wants to increase the prices in the catalog by 4.25%. When I enter an Operator (multiply) of 1.0425 ?? it changes to 1.043. Any ideas on how I can get this to stick? I have over 1000 prices to change.

  • manish k d says:

    I like the script very much, but I want digit coma in indian style i.e. 12,23,334

  • DD says:

    Is it possible to use this script, or a modification of it, on numbers within script labels? I have multiple text frames, each with it’s own script label beginning with a number. I’d like to quickly add 1 to that number for every script label. Any ideas?

  • Ian S says:

    I have used this script in the past and found it to be excellent for price list revision. Having not used it for nearly 12 months the script no longer seems to work?
    I am using Indesign 5.5 on OS 10.7.5 and the script I am using is number_adjuster_1_2-1.jsx
    The script dialog appears but no action is taken after making selection, opening script, setting operation and hitting OK?
    Any ideas?
    Thanks,

    Ian

  • Tobias says:

    Many thanks. Your script worked instantly as desired by me.
    Great!

    Tobias

  • Paul McCarthy says:

    Hello, really clever script. However, it seems to pick up numbers in styles other than the ones I’ve specified. I’m trying to correct footnotes so asked it to only select numbers in the footnote_reference style but it it ended up performing the operation on all numbers in the document. Anyone else come across that?

  • Pierre Paquette says:

    Some (most likely all; I didn’t verify) of my prices end up with only one digit after the period (e.g. 3.5 instead of 3.50) after multiplication by 0.9. I did mention two-digit rounding. Any idea what might have gone wrong?

  • Henning Ryberg says:

    Is there any posibility to make an round of the, calculated number e.g. (12.347,- goes to 12.500,-)

  • MissUed says:

    Wow, thank you so much – this is going to save me a heap of time.

  • Hi,
    How can i reduce the prices with 10%?
    Can someone please help!

    Kind regards
    Jeffrey

  • Will Garner says:

    Hello, David~
    Seems the author of this script is gone gone gone.
    I am using it with CS5.5 on an iMac with OS 10.9.5. Soon as I download it, what do I do? I clicked on the script itself, but there was no way I could see to “load” it into existing scripts in InDesign. When I hit green RUN, there was an error in Line XXX. I dunno which line. : (
    How do I use this beautiful script with my program, please?
    Thank you,
    Will

  • rickb says:

    the script works great but it’s not remembering the last setting. i have 1500 to edit. would be nice if it saved the last settings

  • Ann Farr says:

    This script is brilliant. Just saved my life when an author deleted an endnote from a chapter with 743 endnotes in it. I changed the endnotes to numbered para style and then just used the script minus one. Worked like a dream. Thank you so much for pointing it out.

  • Anne B says:

    WOW! Thanks a thousand times!!!!

  • Martin says:

    Hello David, very nice work! I have a question: What can be done, if I only need to add a number to an existing number with leading zeros, for example add 1800 to 001234? Can I suppress the point ore the comma?

  • Pedro says:

    I reached this page after searching for a script that calculate percentage. Although it seems a pretty script it doesn’t fit my needs. Anyone know or could make a script that calculate percentages based on indesign styles to run on a full document.

    For example:

    Search values on style A
    Search values on style B
    Calculate the difference percentage of both and input on field with style C

    pratical example:
    $199,90 (style A)
    $149,90 (style B)
    25% (submit on style C text box)

  • Mohammed ali says:

    I tried it in cs5, but not works.

  • Eoin says:

    How do I round to the nearest 5 cent or P

  • Gavin Kimber says:

    Hi. I can see it’s been a few years since anyone left a comment, but this script may just save my life!

    I am constantly having to change prices on restaurant menus and this script seems to fit the bill – almost!

    My problem with it is that the rounding doesn’t seem to work and I was wondering if anyone can offer any advice as to what I may be doing wrong.

    I’m using InDesign 2020 and the script seems to work in that. The problem I have is that working on prices seems to round up to the nearest whole decimal.

    If I have prices of 5.95 and want to add 0.25 the result will be 6.2 not 6.20.
    However, 5.10 with 0.25 added does result in 5.35.

    I have tried various combinations of rounding (0, 1, 2, 3, 4) but none seem to have any effect.

    So, I’m halfway there as I can now do a Find & Replace for .2 to .20 etc but it would be nice to get the result.

    If anyone is able to throw any light on this I would be a very happy bunny!

  • >
    Notice: We use cookies on our websites to give you a great online experience. If you keep browsing, we'll assume you're ok with this. For more information, see our privacy policy. By closing this banner, you agree to the use of cookies.I AGREENo