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

  • 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

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

    Now it plays as it should.

  • 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

  • @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.


  • 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

  • @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
    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

  • M5Stack

    @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

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

  • @lukasmaximus

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