Percentage

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
User avatar
jollybv
Flowcode v5 User
Posts: 360
Joined: Thu Feb 12, 2009 5:20 am
Location: Cape Town
Contact:

Percentage

Post 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

mnf
Valued Contributor
Valued Contributor
Posts: 738
Joined: Wed May 31, 2017 11:57 am
Contact:

Re: Percentage

Post 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

User avatar
LeighM
Matrix Staff
Posts: 2043
Joined: Tue Jan 17, 2012 10:07 am
Contact:

Re: Percentage

Post 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

User avatar
LeighM
Matrix Staff
Posts: 2043
Joined: Tue Jan 17, 2012 10:07 am
Contact:

Re: Percentage

Post by LeighM »

As Martins note, try something like
Percentage = (DutyCycle * 100.0) / 255.0

User avatar
jollybv
Flowcode v5 User
Posts: 360
Joined: Thu Feb 12, 2009 5:20 am
Location: Cape Town
Contact:

Re: Percentage

Post by jollybv »

Thanks Guys I've got it working

Post Reply