425,882 Members | 928 Online
Need help? Post your question and get tips & solutions from a community of 425,882 IT Pros & Developers. It's quick & easy.

# Limit any number to four digits?

 P: 10 Hi, I was wondering if someone could help me on this one. Is there anyway to format a string/integer etc so that it always outputs a 4 digits number? thank you. Ron. Oct 12 '06 #1
14 Replies

 100+ P: 1,646 Hi, I was wondering if someone could help me on this one. Is there anyway to format a string/integer etc so that it always outputs a 4 digits number? thank you. Ron. This should help: MsgBox Format(23, "0000") Oct 12 '06 #2

 P: 10 Hi, thanks a lot sire. May I ask what the 23 is for? again, Thank you Ron. Oct 12 '06 #3

 P: 10 it didn't work. here's what I have: Expand|Select|Wrap|Line Numbers variableOne = algorithm_here //for this example sake let's say //variableOne = 0.99999999999999 that's 14 times the number 9 textBox = variableOne   Instead of textBox showing the whole number I'd like it to show only the first 4 thank you Ron. Oct 12 '06 #4

 100+ P: 1,646 it didn't work. here's what I have: Expand|Select|Wrap|Line Numbers variableOne = algorithm_here //for this example sake let's say //variableOne = 0.99999999999999 that's 14 times the number 9 textBox = variableOne   Instead of textBox showing the whole number I'd like it to show only the first 4 thank you Ron. Ah that is different. You must distinguish between either side of the decimal point. You can use the Round() function to limit decimal places to 4: variableOne = Round(variableOne, 4) But that will only work with the decimal point and, especially with the number you are using (0.999999999999), you may keep getting '1' Good luck Oct 12 '06 #5

 P: 10 Hi, and thank you willakawill, that's almost what I want, althouth Round actually rounds the variable variableOne. All I want it to do is to troncate... and I was hoping there would be a way to Format a variable to only hold the first 4 digits, without rounding it. Actually what if I wanted only the four numbers after the decimal places. if variableOne = 0.1234567890 then I would want it to be 0.1234 thanks again, and sorry for changing the question. Ron. Oct 12 '06 #6

 P: 10 variableOne = Format(variableOne, "#.####") thought this would work but it doesn't really work it doesn't add a zero to numbers that have less than 4 digits after the decimal. Ron. Oct 12 '06 #7

 P: 10 this actually worked: variableOne = Format(variableOne, "0.0000") thanks a lot everyone. Ron. Oct 12 '06 #8

 P: 10 Hi again. Using the above formatting technique won't stop variableOne to display in scientific format. any way to always have zeros instead of E? thanks Ron. Oct 12 '06 #9

 100+ P: 1,646 What is the variable type of variableOne? Oct 12 '06 #10

 P: 10 Hi, it's a single, and it needs to be one Thanks. Ron. Oct 12 '06 #11

 100+ P: 1,646 Hi, it's a single, and it needs to be one Thanks. Ron. When you assign the result of the Format function back to the variable you will loose the formatting. If you have to assign the result then you should use a text variable Oct 12 '06 #12

 P: 10 Hi, thanks for the help. I'm still a bit stuck though. Sorry to bother you this much. the variable that doesn't format properly is actually variableThree where variableThree = variableOne + variableTwo I tried putting these two lines but it won't work: variableTwo = format(variableTwo, "0.0000") variableThree = format(variableThree, "0.0000") but variableThree would still appear as scrientific notation. and variableOne, variableTwo, and variableThree are all Single thank you in advance. Ron. Oct 12 '06 #13

 100+ P: 1,646 Hi, thanks for the help. I'm still a bit stuck though. Sorry to bother you this much. the variable that doesn't format properly is actually variableThree where variableThree = variableOne + variableTwo I tried putting these two lines but it won't work: variableTwo = format(variableTwo, "0.0000") variableThree = format(variableThree, "0.0000") but variableThree would still appear as scrientific notation. and variableOne, variableTwo, and variableThree are all Single thank you in advance. Ron. The Format function returns a string. You can't assign it to a single. It won't work. stThisMustBeAStringVariable = Format(anything you want to format here) or MsgBox Format(anthing) or Me.txtTextBox.Text = Format(anything) Use the Format function to format display text after you have completed all calculations. Don't try to store the results of formatting back into a number variable. Oct 12 '06 #14

 P: 10 Hi, thanks everyone. I finally got everything to work properly. it's really appreciated. Thanks again. Ron. Oct 12 '06 #15