Shop OBEX P1 Docs P2 Docs Learn Events
Float to Integer — Parallax Forums

Float to Integer

DiscoveryDiscovery Posts: 606
edited 2014-04-04 10:56 in General Discussion
Is there a "C" function that will convert a Floating point number into an Integer for the Propeller?

Discovery

Comments

  • jazzedjazzed Posts: 11,803
    edited 2014-04-02 15:57
    Hi.

    It's built-in for C programming.
    int main()
    {
      int ipi = 0;
      float fpi = 3.14;
    
      // type-cast float to int. ipi becomes 3
      ipi = (int) fpi;
    
      return 0;
    }
    
  • DiscoveryDiscovery Posts: 606
    edited 2014-04-04 08:25
    Thank you...that code works perfectly.

    Discovery
  • Dave HeinDave Hein Posts: 6,347
    edited 2014-04-04 10:56
    Discovery, (int)fpi will truncate the value to an integer. If you want to round to the nearest integer value you would need to add 0.5 if fpi is positive, or subtract 0.5 if it is negative. I thought I would let you know just in case you wanted to get the nearest integer value.
Sign In or Register to comment.