Question

I have a numericupdown control on my form. The value is 17, I need to store that number into a variable, then convert it to a percentage of 0-255, then convert it to hex.

What I have: 17 Where I need to get: ((17 / 100) * 256 ) - 1 = 42.52 which gets rounded to 43 Then I need to convert that to hex: 43 = 2B

What I don't know/understand.

When I take in the value, what type of variable do I store it in ? string ? Int? double ?

Can someone provide an example please.

Thank you, James

Was it helpful?

Solution

I would go this way:

decimal value = 17m; // or YourNumericUpDownControl.Value
int percent = (int)Math.Round(((value / 100) * 256) - 1, MidpointRounding.ToEven);
string hex = percent.ToString("X");

NumericUpDown.Value is of type decimal (see MSDN), so you should start with a decimal. I've entered 17 here directly, as you did in your example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top