Shop OBEX P1 Docs P2 Docs Learn Events
PBasic Help — Parallax Forums

PBasic Help

Part of my current project, is to clean up some of my pBasic code.

I want to convert 0-255 to a percent of 255.

I have a fuel sender that shows me FULL at DEC 255 and EMPTY at DEC 000.
The variable is FUEL

I am trying [fuel = 100/(255/fuel) . I would like 128 to show me 050 or 50%.

I know I am having integer math problems. I need another method of making this conversion.

Anyone have a suggestion? Sorry, I was surfing during the 60's in So Cal during my high school math classes and my major @ university was music...

Greg

Comments

  • JonnyMacJonnyMac Posts: 9,156
    edited 2017-10-10 03:57
    This is easy:
      percent = 100 * level / 255
    
    By multiplying your level by 100 first, you take care of the fractional problem. The only caveat is making sure that intermediate values in your calculations will fit into 16 bits. In this case, the largest intermediate value is 25500, so that's not a problem.

  • Got it - Tested and working.

    I use variables in a string so for me:

    NemoString(18) = 100 * Nemostring(18)/255

    Thanks Jon!

Sign In or Register to comment.