Hi Guys
I'm trying to display percentage on my LCD display the formula I'm using is Percentage = ((DutyCycle / 255) * 100) what happens it will display 100% (255 / 255) * 100 but will not display any other value eg Percentage = (( 253 / 255) * 100 ) which should give me 99.21 % except it gives me 0%, Percentage is a floating variable. Any ideas what I'm doing wrong
Percentage
-
- Valued Contributor
- Posts: 1189
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 439 times
- Contact:
Re: Percentage
You need to force use of FP numbers so "253.0 /255 * 100" should work. It's currently calculated as an integer then converted on assignment. (note the .0 does the magic)
Martin
Martin
- LeighM
- Matrix Staff
- Posts: 2178
- Joined: Tue Jan 17, 2012 10:07 am
- Has thanked: 481 times
- Been thanked: 699 times
- Contact:
Re: Percentage
Values on the right hand side of the equals sign need to be floats as well
The right hand side is evaluated first, irrespective of the variable type on the left
The right hand side is evaluated first, irrespective of the variable type on the left