Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Restrict Number to 2 decimals 3

Status
Not open for further replies.

thakur

Programmer
May 31, 2001
9
IN
I want a textbox to show a two decimal number as 133.33 how can i do that?
 
Hi,

Use the following line:

text1.Text = Format(text1.Text, "0.00")

You might want to put it in the lost focus event.

Madlarry
 
In my oppinion you have two posibilities:

1. Use the idea of madlarry, but take care and put it onot on LostFocus event but in the Validate Event where you can cancel the users entry if it does not fit your request(e.g. 2 decimals)

2. Use an OCX - that comes with VB - Microsoft Masked Edit Control - it has many properties, but the most important are Mask and PropmptChar. It has also an event called ValidationError that you can use for your purpose.

Hope of being of some help, s-)

Blessed is he who in the name of justice and good will, shepards the week through the valley of darknees...

 
You may decide to write a sub routine to check the textbox for the number of characters after the decimal place... I wrote a sub using frm.activecontrol. I pass the form and keyascii that has been entered on the keypress or keydown or keyp events. This allows you to not have to deal with the masked edit, which can be quite annoying. I also pass several additional parameters in my sub for allowing "+" & "-", allowing the decimal, and the number of digits before or after the decimal. If you want to view my code, please email me at neehouse@home.com
 
And, if you don't want to use a masked edit or format$() function, you can multiply the number by 100, assign it to a Long, assign it back to a Double, and then divide by 100.
[tt]
Dim lTemp as Long

lTemp = dPrice * 100
dPrice = lTemp \ 100
[/tt]

It may be faster in some instances, since the Format$() function isn't exactly a lightweight.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top