Groups | Blog | Home
all groups > vb.net controls > november 2006 >

vb.net controls : textbox containing percentage


Steve Arndt
11/16/2006 2:35:09 PM
How can I display a value in a textbox as a percentage while still using the
value as a decimal.

For instance, the user enters 33. When I leave the textbox, I was using
Format.Percent(txtbox, 2) to format the value as a percentage. But it
changes to 3300.00% instead of 33.00%.

I tried setting a string variable using String.Format("{0:###.00'%}",
txtbox.Text), and then setting the txtbox to the string variable, but this
hasn't worked so far..

And if I use Format.Percent, I get an error that says I can't convert
"00.0%" to Double, whenever I try to set a local decimal variable to the
txtbox value.

Thanks in advance...

Steve

Tim Patrick
11/16/2006 11:18:38 PM
That's just the nature of percentages. 33 is in fact 3300%. 0.33 is 33% (about
1/3 of a whole 1.00). I would multiply the user-entered number by 0.01 before
formatting it as a percent. You could also just tack on the strong ".00%"
if that's good enough for your needs.

-----
Tim Patrick - www.timaki.com
Start-to-Finish Visual Basic 2005

[quoted text, click to view]

AddThis Social Bookmark Button