Community
    • Login

    Percentage Calculator

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    5 Posts 3 Posters 653 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Moustafa IbrahimM
      Moustafa Ibrahim
      last edited by

      Hello, I’m trying to adjust a lot of numbers in many files and I’m doing it the hard way, copying the number putting it in a percentage calculator website then copying the new number and adding it to the file, so I was wondering if there is a plugin that would make it easier to do that inside of notepad++? like for example choosing the percentage I want in plugin settings then when I highlight the number in file I press a shortcut that will replace it with the adjusted number.
      thank you.

      Alan KilbornA CoisesC 2 Replies Last reply Reply Quote 2
      • Alan KilbornA
        Alan Kilborn @Moustafa Ibrahim
        last edited by

        @Moustafa-Ibrahim said in Percentage Calculator:

        if there is a plugin that would make it easier to do that inside of notepad++?

        There is. It’s called PythonScript; see HERE for more info.

        Some script code that would do what you want could be something like the following:

        # -*- coding: utf-8 -*-
        from __future__ import print_function
        
        from Npp import *
        
        percent = 12.3  # 12.3%, for example
        dec_places_in_result = 2  # 2 decimal places, for example
        if not editor.getSelectionEmpty():
            try:
                selected_float = float(editor.getSelText())
            except ValueError:
                pass
            else:
                editor.replaceSel('{:.{}f}'.format(selected_float * percent / 100.0, dec_places_in_result))
        
        1 Reply Last reply Reply Quote 3
        • CoisesC
          Coises @Moustafa Ibrahim
          last edited by

          @Moustafa-Ibrahim said in Percentage Calculator:

          Hello, I’m trying to adjust a lot of numbers in many files and I’m doing it the hard way, copying the number putting it in a percentage calculator website then copying the new number and adding it to the file, so I was wondering if there is a plugin that would make it easier to do that inside of notepad++? like for example choosing the percentage I want in plugin settings then when I highlight the number in file I press a shortcut that will replace it with the adjusted number.

          It is possible that Columns++ could help you.

          If your numbers are arranged in columns, you can use Calculate to apply a calculation to entire columns at once. The results are placed in a new column; of course, you can then just delete the original column if you wanted replacement.

          If your numbers can be identified by a regular expression, you can use formula replacement in the Search dialog — for example, searching for Aclev=(\d+) and replacing with Aclev=(?=reg(1)*.9) would replace the number with 90% of the number.

          Alan KilbornA 1 Reply Last reply Reply Quote 2
          • Alan KilbornA
            Alan Kilborn @Coises
            last edited by

            @Coises said in Percentage Calculator:

            searching for Aclev=(\d+) and replacing with Aclev=(?=reg(1)*.9) would replace the number with 90% of the number.

            Can this be tied to a shortcut keycombo? Can the number of decimal digits be controlled?

            CoisesC 1 Reply Last reply Reply Quote 2
            • CoisesC
              Coises @Alan Kilborn
              last edited by Coises

              @Alan-Kilborn said in Percentage Calculator:

              Can this be tied to a shortcut keycombo?

              Not really, since it’s a dialog-based function; but the dialog is non-modal, so you can leave it open while working. I suppose it would be possible to use the dialog accelerator keys while the dialog retains focus, but that would be more clumsy than just using the mouse, I think.

              The PythonScript solution you suggested is probably superior if you want to manually select instances to convert rather than do a batch, either by column or by regular expression.

              Can the number of decimal digits be controlled?

              Yes. That’s described in the help for Formulas. The short version is that you enter it like this:
              (?=format:formula)
              where the format gives the minimum number of integer digits, and if decimals are wanted, a decimal point and the number of decimal places to be shown. Specifics are given in the help, but as examples, you could use:
              (?=6:reg(1)*.9)
              if you wanted leading zeros to make a minimum of six digits, and no decimals; or:
              (?=2.3:reg(1)*.9)
              if you wanted always at least two digits to the left of the decimal point and exactly three digits to the right; or:
              (?=0.-4:reg(1)*.9)
              if you wanted up to four digits after the decimal point but with trailing zeros suppressed, no decimal point if there are no non-zero digits after the decimal point, and no zero before the decimal point when the integer portion is zero and the decimal portion is not zero.

              The default when using just (?=formula) is (?=1.-6:formula), meaning up to six decimals, suppress trailing zeros, suppress decimal separator if nothing follows, no leading zeros except that at least one digit is required before the decimal point, even if it is a zero.

              1 Reply Last reply Reply Quote 3
              • First post
                Last post
              The Community of users of the Notepad++ text editor.
              Powered by NodeBB | Contributors