user310291
user310291

Reputation: 38190

How to convert .NET Trackbar control integer value to floating-point percentage

I always get 0 for percentage when I do this:

int trackBarValue = trackBar.Value;
float percentage = trackBarValue / 100;

What's wrong with my code?

Upvotes: 1

Views: 3013

Answers (1)

Andrew Cooper
Andrew Cooper

Reputation: 32576

The problem is you're doing an integer division, which is truncated. Try this:

  int trackBarValue = trackBar.Value;
  float percentage = trackBarValue / 100.0;

This will do a floating point division, and given you the result you want.

Upvotes: 3

Related Questions