Community
    • Login

    Unit Conversion Plugin

    Scheduled Pinned Locked Moved Notepad++ & Plugin Development
    18 Posts 3 Posters 15.3k 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.
    • Jared HowardJ
      Jared Howard
      last edited by

      Lines are typically separated by a space, however can vary as to if they have a decimal point or not. They will most likely always be a variation of the below.

      2.99, 10,
      3.98, 9,

      0.25, 0.39,
      0.25, 0.39,

      1 Reply Last reply Reply Quote 0
      • Claudia FrankC
        Claudia Frank
        last edited by

        Can I safely assume that you always use ALT+SHIFT to select the values?
        This is critical because selecting with ALT+SHIFT and arrow right etc. results (taking your example) in

        Selection:0 Values:2.99, 10,
        Selection:1 Values:3.98, 9,
        Selection:2 Values:
        Selection:3 Values:0.25, 0.39,
        Selection:4 Values:0.25, 0.39,

        whereas using CTRL+ALT would result in

        Selection:0 Values:2.99, 10,
        3.98, 9,

        0.25, 0.39,
        0.25, 0.39,

        As you see, the first would report 5 selections, each line as one selection where as
        the second reports one selection for all 5 lines.

        If using ALT+SHIFT we need to check if line has valid values (empty lines issue),
        then split by comma, do conversion for each value and rebuild string.
        Should we go this way?

        Cheers
        Claudia

        1 Reply Last reply Reply Quote 0
        • Jared HowardJ
          Jared Howard
          last edited by

          Correct, I always use ALT+SHIFT for selection. Your proposal sounds like the correct way to proceed.

          Thank you again for your assistance. I am more versed in Pascal and am not very fluent yet with Python.

          1 Reply Last reply Reply Quote 0
          • Claudia FrankC
            Claudia Frank
            last edited by Claudia Frank

            Hi,

            I did some tests(empty line, just a comma, double comma) and I guess this should do the trick

            editor.beginUndoAction()
            
            def conv_mile_kilometers(miles):  
                list_of_values = miles.split(',')
                km = ''
                for value in list_of_values:
                    if len(value) > 0 and value.isspace != True:
                        km += str(float(value) / 1.6) + ','
                return km
            
            for i in range(editor.getSelections()):
                start = editor.getSelectionNStart(i)
                end = editor.getSelectionNEnd(i)
                word = editor.getTextRange(start,end)
                if len(word) > 0 and word.isspace != True:
                    editor.setTarget(start, end)
                    editor.replaceTarget('{0}'.format(conv_mile_kilometers(word)))    
                
            editor.endUndoAction() 
            

            Cheers
            Claudia

            1 Reply Last reply Reply Quote 0
            • Claudia FrankC
              Claudia Frank
              last edited by

              Correction:-(

              Unfortunattely I cannot edit my post so

              both isspace items need to be changed to isspace()
              because they are functions.

              Full code

              editor.beginUndoAction()
              
              def conv_mile_kilometers(miles):  
                  list_of_values = miles.split(',')
                  km = ''
                  for value in list_of_values:
                      if len(value) > 0 and value.isspace() != True:
                          km += str(float(value) / 1.6) + ','
                  return km
              
              for i in range(editor.getSelections()):
                  start = editor.getSelectionNStart(i)
                  end = editor.getSelectionNEnd(i)
                  word = editor.getTextRange(start,end)
                  if len(word) > 0 and word.isspace() != True:
                      editor.setTarget(start, end)
                      editor.replaceTarget('{0}'.format(conv_mile_kilometers(word)))    
                  
              editor.endUndoAction() 
              

              Cheers
              Claudia

              1 Reply Last reply Reply Quote 0
              • Jared HowardJ
                Jared Howard
                last edited by

                Excellent it works great, thank you!

                The last question I have regarding this is how do I format the number of decimals that the values get returned as when converted?

                1 Reply Last reply Reply Quote 0
                • Claudia FrankC
                  Claudia Frank
                  last edited by

                  Hello Jared,

                  I’m sorry I don’t understand your question could you gimme an example
                  of what you want to do?

                  Thanks
                  Claudia

                  1 Reply Last reply Reply Quote 0
                  • Jared HowardJ
                    Jared Howard
                    last edited by

                    Sorry I see my wording was not clear.

                    Once I convert say millimeters to inches is there a way to control the number of decimal places?

                    1.33 inches converted to metric is = 33.782

                    I want the value to only display 2 decimal places, 33.78. How is that accomplished?

                    Thanks again!

                    1 Reply Last reply Reply Quote 0
                    • Claudia FrankC
                      Claudia Frank
                      last edited by

                      Hello,
                      this can be done using format’s strength.
                      Within the try block we need to change the following line

                      km += str(float(value) / 1.6) + ','
                      

                      to

                      km += '{0:.2f},'.format(float(value) / 1.6)
                      

                      Short explanation, the placeholder {0} receives the result from the
                      calculation float(value) / 1.6.
                      By specifying the colon after the zero, the format function gets informed
                      that the result needs to be reformatted.
                      The dot after the colon defines precision formatting,
                      the number 2 specifies only 2 decimal places and the letter f is to inform that it is
                      a float which needs to be formatted.

                      But we can go one step further, if you are interested in keep the number of
                      decimal places of the input and the output value the same then
                      we can replace the km += … line with the following two lines.

                      decimal_places = str(value)[::-1].find('.')
                      km += '{0:.{1}f},'.format((float(value) / 1.6),decimal_places)
                      

                      as you see, the 2 is now replaced with another placeholder{1} which
                      get the value of decimal_places variable, which itself gets the value
                      from a stringified value where we search for the dot from right to left (that is what the -1 stands for).

                      Cheers
                      Claudia

                      1 Reply Last reply Reply Quote 0
                      • Jared HowardJ
                        Jared Howard
                        last edited by

                        Outstanding!

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