Having troubles rounding certain numbers on a line
-
Hello notepad++ friends. I am trying to help my dad with some of his work but I cant seem to get the right regex formula to use. He wants to round numbers to 1 decimal place but only one number in a line and not all. Sorry for my bad english, hopefuly the example below can help you understand
He wants this:
<circle cx="645.39696961967" cy="783.9411254969543" r="2.0931372549019605" fill="#ffffff" fill-opacity="1"/> <circle cx="645.39696961967" cy="758.4852813742385" r="0.08372549019607813" fill="#ffffff" fill-opacity="1"/> <circle cx="645.39696961967" cy="741.5147186257615" r="0.48241830065359453" fill="#ffffff" fill-opacity="1"/>
to become this…
<circle cx="645.39696961967" cy="783.9411254969543" r="2.1" fill="#ffffff" fill-opacity="1"/> <circle cx="645.39696961967" cy="758.4852813742385" r="0.1" fill="#ffffff" fill-opacity="1"/> <circle cx="645.39696961967" cy="741.5147186257615" r="0.5" fill="#ffffff" fill-opacity="1"/>
notice only the number after “r=” has been rounded in each line but the others ones have been left untouched.
Is there a way to round only the number after “r=”?
Iv’e been up all night trying to solve it but I keep getting errors. If anyone can help me, I would be very grateful and will send you money for a drink if you’d like
Thanks again
-
@abdul-ali said in Having troubles rounding certain numbers on a line:
Is there a way to round only the number after “r=”?
I don’t think it’s possible with a regex formula. Certainly something like
(r="\d+\.\d)\d+1
and replace of\1
will truncate but as regex doesn’t have the ability to round I don’t think that’s possible.My thoughts were to grab the first number after the DOT and then test the next number but straight away I haven’t actually dealt with the first number, so no method of incrementing it. Then there is the highest number, namely xx.99, that should increment the last number before the DOT, which may then need the previous number also changed, and so on.
Search the previous posts, you might find one with some relevance, and one I found actually includes some pythonscript code that might be a better idea.
https://community.notepad-plus-plus.org/topic/15100/regex-rounding-numbersTerry
-
@terry-r said in Having troubles rounding certain numbers on a line:
Certainly something like (r="\d+.\d)\d+1 and replace of \1 will truncate
Sorry a
1
got in there somehow, it should be(r="\d+\.\d)\d+
with\1
.Terry
-
As @terry-r and my previous post he linked you to explain, regex is not very good at rounding.
With your requirements, and with what I’ve learned in the intervening years, I would use the following script and the PythonScript plugin to solve your problem; the installation and running instructions are embedded in the script.
# encoding=utf-8 """this uses the "add_1" style search and replace in Notepad++ INSTRUCTIONS: 1. Install PythonScript * Plugins > Plugins Admin * Click ☑ PythonScript checkbox until checked * click INSTALL * restart Notepad++ as necessary 2. Create new PythonScript script * Plugins > PythonScript > New Script * Give it a reasonable name, like round_r.py * Paste in the code from this black box * Save 3. Switch to the document where you want to search/replace 4. Plugins > PythonScript > Scripts > round_r To assign a keyboard shortcut: 1. Plugins > PythonScript > Configuration 2. In the User Scripts, click on round_r.py 3. Click the left ADD button (above Menu Items) * This adds "round_r" to the main Plugins > PythonScript menu, rather than requiring you to dig down into the Scripts submenu 4. Exit Notepad++ and restart * This is required for ShortcutMapper to be able to see the new entry in the PythonScript menu 5. Settings > ShortcutMapper > Plugin Commands 6. Filter by round_r 7. Click on round_r, then click the MODIFY button and set the keyboard shortcut in the dialog PythonScript has documentation in its Context-Sensitive Help """ from Npp import * def round_1(m): rounded = round(float(m.group(0)), 1) # round to 1 decimal place return str(rounded) editor.rereplace(r'(?<=r=")\d+\.?\d*(?=")', round_1)
When I ran this script on your “before” data, I got your “after” data.
-
Thank you so much to both of you. Both solutions worked well and is going to save me so many hours. Do you guys have a paypal account, I would like to send you £10 each for your efforts. Thanks again