Page 1 of 1

Percentage

Posted: Tue Jun 30, 2020 3:59 pm
by jollybv
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

Re: Percentage

Posted: Tue Jun 30, 2020 4:05 pm
by mnf
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

Re: Percentage

Posted: Tue Jun 30, 2020 4:07 pm
by LeighM
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

Re: Percentage

Posted: Tue Jun 30, 2020 4:12 pm
by LeighM
As Martins note, try something like
Percentage = (DutyCycle * 100.0) / 255.0

Re: Percentage

Posted: Tue Jun 30, 2020 4:49 pm
by jollybv
Thanks Guys I've got it working