Copy link to clipboard
Copied
Hello. Is there a way to create a style or a grep that will automatically convert text to a percentage format? So for example if I type "25.5" into a text box, it automatically adds a "%" at the end to make it "25.5%"? Similar to how it works in cell formatting in Excel? I have a file that includes a lot of numbers that need to be typed in so not having to type the % character or paste it in every time would save me some time. Thank you.
GREP styles (and character styles generally speaking) do not add any character, they format the text so it won't be possible that way. But you could run a find-change to add a percentage sign after all the numbers formatted in a defined style.
Copy link to clipboard
Copied
I imagine there will be numbers that are not suppose to be formatted as a percentage. If this is the case, is there anything that distinguishes a percent number from a non-percent number?
Copy link to clipboard
Copied
It would be an entire table column or row so I'd like to create a paragraph or character style that when applied to any number, it would convert it to a percentage. That way I can set all numbers in a particular row to be percentages.
Copy link to clipboard
Copied
GREP styles (and character styles generally speaking) do not add any character, they format the text so it won't be possible that way. But you could run a find-change to add a percentage sign after all the numbers formatted in a defined style.
Copy link to clipboard
Copied
That could work, thank you. Also is there a way to format a table cell to convert a number to a specific format like converting 1200 to 1,200 or 100 to 100.00 like in Excel? Just trying to save time from typing commas, decimals, etc.
Copy link to clipboard
Copied
No, InDesign tables can't be compared to Excel tables in this matter. But once again, a find-change query could do what you want to get.
Copy link to clipboard
Copied
Yes, I could add a ".00" to the end of all numbers in a style. Thank you.
Do you know how I would do a find change to add a comma in all long numbers like converting 1234 to 1,234 and 10234 to 10,234? It would need to apply to all numbers of a certain length I guess. I know that's asking a lot, but I figured I'd ask just in case.
Copy link to clipboard
Copied
How many digits are there in your numbers?
This query works with 4 to 6 digits like 1234, 10234, 100234
Copy link to clipboard
Copied
This is amazing. It worked. Thank you very much! Would there happen to be a grep resource available somewhere that can be kind of an index of different grep values you can use?
Copy link to clipboard
Copied
Would there happen to be a grep resource available somewhere that can be kind of an index of different grep values you can use?
@Peter Kahrel which is very active on this forum wrote one book (maybe more than one) about GREP.
Copy link to clipboard
Copied
Thank you so much. I will purchase it. This thread has been super-helpful. Appreciate it.
Copy link to clipboard
Copied
I'd start with 2 greps
Find
\d\K\d{3}$
Change to
,$0
2nd grep
\d\K\d{3}(?=,)
Change to
,$0
Since it's the same change you can run them both at the same time
\d\K\d{3}$|\d\K\d{3}(?=,)
Change to
,$0
But you have to do multiple find/changes the larger the number gets
1
10
100
1000
10000
100000
1000000
10000000
1000000000
10000000000
have to do multiple find/changes until they all are changed
1
10
100
1,000
10,000
100,000
1,000,000
10,000,000
1,000,000,000
10,000,000,000
-----------
This script seems to do the trick for the commas
Haven't fully tested and if you need it to do more please ask
// InDesign Script: Add commas to numbers
// Ensure an active document is open
if (app.documents.length === 0) {
alert("No document is open!");
} else {
var doc = app.activeDocument;
// Clear previous GREP search settings
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;
// Set up GREP to find sequences of digits
app.findGrepPreferences.findWhat = "\\d+";
// Find all matches in the document
var foundNumbers = doc.findGrep();
// Function to add commas
function addCommas(numStr) {
// Regular expression to insert commas
return numStr.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
// Process each found number
for (var i = 0; i < foundNumbers.length; i++) {
var found = foundNumbers[i];
// Get the current number string
var original = found.contents;
// Apply formatting only if it doesn't already contain commas
if (original.indexOf(",") === -1) {
var formatted = addCommas(original);
// Replace the text with the formatted version
found.contents = formatted;
}
}
// Clear GREP preferences after processing
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;
alert("Finished formatting numbers!");
}
@jmlevy beat me to it - but hopefully it helps still