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...



  • 2 Comments sorted by Date Added Votes
  • JonnyMacJonnyMac Posts: 5,940
    edited October 2017 Vote Up0Vote Down
    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.

    Jon McPhalen
    Hollywood, CA
    It's Jon or JonnyMac -- please do not call me Jonny.
  • 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.