How to work with float values

Hi forum members,

I would have a basic question about the handling of float values...

My problem:

I receive an array with byte values. One byte represents the absolut value the other byte represents the values after comma.

I tried like this:

var Depth = 0.0;

Depth = value[6]; // Absolut value
Depth = Depth + (value[7]/100); // Value after comma

But seems that the assignment of byte changes the datatype. Casting like on C also not possible.

This is really basic, but I did not find any useful answer in the internet.

Thanks for reply!

Best regards Sven

  • With a byte array and decode number, you can specify the number format.  See https://developer.garmin.com/connect-iq/api-docs/Toybox/Lang.html

    I do something like you are in some cases where one byte is the integer part and the second byte, the decimal part.  In your line 4, try 100.0 and not just 100.  With just 100, you get a number back and what you probably see is 0.  With 100.0, you get a float, so maybe something like 0.17

    For example, I get the temperature in a byte array.  In C, the integer part is a signed 8 bit value, and all reasonable temps work (-130C isn't reasonable!).  Then for the decimal part it's always 0 to 99., so I'll see the correct value with 100.0

  • Yes,

    this solved my issue!

    Thanks a lot.

    Issue can be closed.

  • To be clear, for anyone else reading this, division in Monkey C works like division in C or Java.

    Given an expression such as x / y:

    - if both x and y are integers, then integer division is performed, which means the result is truncated to an integer (e.g. 5 / 2 = 2)

    - if either x or y are floating point, then float division is performed (e.g. 5 / 2.0 = 2.5)