Welcome to the Parallax Discussion Forums, sign-up to participate.

# PBasic Help

Posts: 132
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

• Posts: 5,940
edited October 2017
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.
• Posts: 132
Got it - Tested and working.

I use variables in a string so for me:

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

Thanks Jon!