🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Convert to Integer and test of integer give an Error (solved)

    Scheduled Pinned Locked Moved UIFlow
    9 Posts 3 Posters 15.3k Views 1 Watching
    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 Offline
      Peter
      last edited by Peter

      Hello,

      I am trying to convert a floating point number into an integer or to test if the number is a floating point number. Both attempts end in an error message as soon as a floating point number is really present.

      Here you should see pictures of Blocky, but doesn't work.
      alt text
      alt text
      alt text

      Does anyone have a solution or what am I doing wrong?

      Thanks a lot
      Peter

      I have found my mistake. A string was passed to the function and not a float.

      Now it plays as it should.

      1 Reply Last reply Reply Quote 0
      • T Offline
        Thrasher
        last edited by

        You can't, integer supports only complete values without " . "
        Probably adding something like 0.5 to float first and than trying to make it integer will help

        P 1 Reply Last reply Reply Quote 0
        • P Offline
          Peter @Thrasher
          last edited by

          @thrasher

          Thank you Thrasher,
          I know how to round up. I am interested in testing whether a variable contains an integer or a floating point number. I wanted to cut off the fractional part by converting it to integer, subtract the integer from the number of unknown type and if the result is 0 then it is an integer.

          But since the value is probably passed as a string I have to find another way.

          Peter

          1 Reply Last reply Reply Quote 0
          • T Offline
            Thrasher
            last edited by

            I never tried myself, but theoretically you can convert float to char string, than split it using C string instructions and than put desired part into integer. Im not sure blockly allows to do such tricks

            P 1 Reply Last reply Reply Quote 0
            • P Offline
              Peter @Thrasher
              last edited by

              @thrasher

              i have tried further and found that the value is not given as text after all. But if I convert it to text and then count the number of "." I get the desired result. In Python there is isinstance() for type determination. But I did not find this in Blockly.

              Peter

              1 Reply Last reply Reply Quote 0
              • T Offline
                Thrasher
                last edited by

                @Peter
                I'm sorry, my knowledge is limited with arduino/C, Im at the very beginning with learning python as I literally very short with time.
                However, I don't suggest you to keep trying with blockly as block-building style coding is very limited.
                Better give raw python/c++ a try, as far as I know python will manipulate with your value like its an object unless you force it to use specific data type

                P 1 Reply Last reply Reply Quote 0
                • lukasmaximusL Offline
                  lukasmaximus
                  last edited by

                  @Peter if you want to add a regular python function that is supported by micropython but not included in blocks you can use the execute block found in the advanced section

                  P 1 Reply Last reply Reply Quote 0
                  • P Offline
                    Peter @Thrasher
                    last edited by

                    @thrasher
                    I would like to try out the possibilities and limits of Blockly.

                    1 Reply Last reply Reply Quote 0
                    • P Offline
                      Peter @lukasmaximus
                      last edited by

                      @lukasmaximus

                      Thanks for the tip. I hadn't thought of that. I thought I would have to construct my own block.

                      Peter

                      1 Reply Last reply Reply Quote 0

                      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                      With your input, this post could be even better 💗

                      Register Login
                      • First post
                        Last post