Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
andysmith1965
Level 9

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?

0 Kudos
3 Replies
necrotek
Level 11

Re: INT Conversion

though "INT" should return what you are expecting try "CINT"

https://developer.roku.com/docs/references/brightscript/language/global-math-functions.md

0 Kudos
andysmith1965
Level 9

Re: INT Conversion

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

Brightscript Debugger> p CINT(20715915.0)
20715916

0 Kudos
RokuKC
Roku Employee
Roku Employee

Re: INT Conversion

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.

 

0 Kudos