Forum Discussion

andysmith1965's avatar
andysmith1965
Streaming Star
5 years ago

INT Conversion

HI
I'm getting an issue where an api response is returning a float for a user id, i.e
{"user_id"  :  20715915.0}

I want to convert this to an INT by applying:

INT(20715915.0)
but this returns 20715916
instead of 20715915

Can anyone help me here?

3 Replies

    • andysmith1965's avatar
      andysmith1965
      Streaming Star

      Thanks for the quick response but CINT also returns the same, I've tried that already.

      Brightscript Debugger> p CINT(20715915.0)
      20715916

      • RokuKC's avatar
        RokuKC
        Roku Employee

        This is a limitation of the 32-bit IEEE floating point representation used by the BrightScript Float type, and the global functions Int and CInt which take a parameter of that type.

        In your own calculations you may be able to use the BrightScript Double (and LongInteger) types if you need greater precision.

        Unfortunately it is a known issue that for backward compatibility reasons the ParseJSON function will emit a Float value rather than Double for values that fit in the Float range, which may result in the IEEE precision/rounding issues with conversions such as you have found.