0

I'm using a library where I need to set a value on a variable and it only accepts float type, but the TrackBar Control only accepts Int32 for its Value property.

So let's say that the Min value of TrackBar is 0 and Max value is 100. And the Current value of TrackBar is 67; how do I convert that value to its equivalent in float? So it should become something like 0.067, if I'm correct.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
jay_t55
  • 11,362
  • 28
  • 103
  • 174
  • `.67` is probably what you want. `.067` means 67 thousandths not 67 hundredths. – Hogan Aug 11 '14 at 18:35
  • @Hogan If you're declaring the float, you should have it `=0.67f`. See [Why is the “f” required when declaring floats?](http://stackoverflow.com/questions/11519743/why-is-the-f-required-when-declaring-floats) – Scott Solmer Aug 11 '14 at 18:38
  • 2
    @Okuma.Scott - no idea what you are talking about, I'm not declaring anything -- I'm just pointing out what `.067` and `.67` mean. – Hogan Aug 11 '14 at 18:41
  • @Hogan Okay, it wasn't clear. – Scott Solmer Aug 11 '14 at 18:42

2 Answers2

2

To convert int to float just cast:

(float)trackBar.Value;

or

Convert.ToSingle(trackBar.Value);

Note that if you need to change range you need to perform at least division. I.e. in your case it looks like value is percentage, so divide by 100.0:

trackBar.Value / 100.0f;

Note that if you use 100 than result will be wrong as using integer division ( trackBar.Value / 100 will always be 0).

In general conversion for integer in 0-n range to (mixValue, maxValue) range:

 (trackBar.Value /( (float) maxValue - minValue)) + minValue;
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Jon
  • 3,230
  • 1
  • 16
  • 28
  • 1
    Neither of those work. The minimum value of this float variable is 0 and maximum is 1. I need to get all numbers inbetween 0 and 1. (i.e.: 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 1) but with both your examples, the result is always the same: 1. – jay_t55 Aug 11 '14 at 18:38
  • @JasonPezzimenti, can you edit your post to add this information, along with a quick code example? Highlight what each of the data types are. – gunr2171 Aug 11 '14 at 18:40
  • Then just divide by 100! Convert.ToSingle(trackBar.Value) / 100.0; – Jon Aug 11 '14 at 18:40
  • -1: please make sure to edit in comments to provide complete answer. Thanks you. – Alexei Levenkov Aug 11 '14 at 18:53
  • @Mangist - I've edited your post with percentage/range comments to show what I thing would be ok answer. Feel free to rollback and put your own version. – Alexei Levenkov Aug 11 '14 at 19:09
2

Divide the integer value by 100 as a float to get the percentage as a float. This will cause the value to be normalized to fall between 0 and 1 as a decimal value:

float trackBarPercentage = trackBar.Value / 100f;

Dividing the integer value by a float value will cause the right-hand side to automatically be casted to the float type. See the MSDN article Casting and Type Conversions for more info.

avanek
  • 1,649
  • 12
  • 20