• Login
Community
  • Login

remove number over four un json

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
7 Posts 2 Posters 428 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.
  • P
    Pouemes44
    last edited by Jun 25, 2021, 1:05 PM

    Hello, i have a json file with geometry points
    to get a lightweight file i would to have not more four number after the coma, so remove the last two if there six nunbers etc
    how can i do
    thank a lot for your help

    1 Reply Last reply Reply Quote 0
    • P
      Pouemes44
      last edited by Jun 25, 2021, 1:14 PM

      to give you more explanations

      in my json i have geometry points , like this

      [x.xxxxxx,yy.yyyyyy] 
      

      before the . (point) it can be one or two x (same for y),
      and these x and y can be with -
      before … after the coma the number of x and y varies

      i would to keep only 4 numbers after the . (point)

      so

      [-95.960683,35.622691]
      

      become

      [-95.9606,35.6226]
      

      but

      [9.6021,-45.6081]
      

      stay

      [9.6021,-45.6081]
      

      if you have ideas
      thanks

      P 1 Reply Last reply Jun 25, 2021, 1:25 PM Reply Quote 1
      • P
        PeterJones @Pouemes44
        last edited by PeterJones Jun 25, 2021, 1:26 PM Jun 25, 2021, 1:25 PM

        @Pouemes44 ,

        Thank you for the example, because I would not have interpreted your original question in that way at all.

        So what you’re really saying is that you want to truncate any floating-point numbers to only four digits after the decimal. (Thank you for wanting truncation; that’s much easier than rounding – in fact, for the general case, true rounding requires a scripting solution rather than pure regex.)

        • FIND = (?<=\d\.\d{4})\d+
        • REPLACE = empty
        • SEARCH MODE = regular expression

        The FIND is using a lookbehind assertion (?<=...) to say that “what’s inside this assertion must come before”. Inside the assertion, it’s looking for a digit \d followed by a literal period \. (the \ is required to make the . be treated as a literal character instead of the special regex meaning of “. matches any character”), followed by four digits \d{4} (where {4} is a multiplying operator ). After the assertion, it’s looking for one or more digits \d+ (+ is another multiplying operator ). Because of the lookbehind, the digit+dot+four-digits aren’t part of the “match”, so only the 5th digit and beyond are part of the match – so only 5th digit and beyond get replaced with nothing (since the REPLACE is empty).

        [-95.960683,35.622691]
        [9.6021,-45.6081]
        [3.1415,-2.718281828]
        

        becomes

        [-95.9606,35.6226]
        [9.6021,-45.6081]
        [3.1415,-2.7182]
        

        Thank you again for the example data: showing examples of what should be changed and what shouldn’t be changed is very useful to give us things to test our example regular expressions before posting.

        1 Reply Last reply Reply Quote 0
        • P
          Pouemes44
          last edited by Jun 25, 2021, 1:29 PM

          thanks Peter
          I will test

          1 Reply Last reply Reply Quote 0
          • P
            Pouemes44
            last edited by Jun 25, 2021, 1:32 PM

            seems to work very well thanks Peter

            1 Reply Last reply Reply Quote 1
            • P
              Pouemes44
              last edited by Jun 25, 2021, 2:02 PM

              done well works
              and now

              in some i have a space after the coma

              examples
              [-95.9606, 35.6226] or [5.4208, -147.1686]
              how to remove this space
              to get
              [-95.9606,35.6226] or [5.4208,-147.1686]

              thanks

              1 Reply Last reply Reply Quote 0
              • P
                Pouemes44
                last edited by Jun 25, 2021, 2:30 PM

                i have made this on you model, which seems to work

                search

                (?<=\d\.\d{4})\, 
                

                replace

                ,
                
                1 Reply Last reply Reply Quote 2
                6 out of 7
                • First post
                  6/7
                  Last post
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors