Formatting/Rounding numbers - text box 
Author Message
 Formatting/Rounding numbers - text box

I'm really a novice at Visual Basic.  Thanks to everyone who's helped me so
far.  Here's another question!

I'm having trouble rounding the results of my calculations which are
displayed in a text box.  I'm trying to round the numbers so that the
results are more easy on the eyes.  (ex:  0.5 --> shows up as
0.500000000000001)

Any suggestions on the proper syntax?

Ronnie

Dim Y() As Double
Dim Z() As Double

For Counter = 0 To N - 1
    ReDim Y(Counter) As Double
    ReDim Z(Counter) As Double
    Y(Counter) = radius * Cos(theta * (2 * Counter + 1))
    Z(Counter) = radius * Sin(theta * (2 * Counter + 1))

    format (Y(counter), "#.####")

    corners.Text = corners.Text & ("(0, " & Y(Counter) & ", " & Z(Counter) &
")") & vbCrLf

Next Counter

--
Please reply to rwtam (at) engmail (dot) uwaterloo (dot) ca



Sat, 22 Sep 2001 03:00:00 GMT  
 Formatting/Rounding numbers - text box
Also, each time I go through the loop, the program seems to forget previous
values saved to the array.  For example, the first time through the loop,
Y(0) is given a value.  If I put a "print Y(0)" command in each time it
loops through, the first time the correct value is displayed and then 0's
are printed for each subsequent pass through the loop.

What's wrong with my code?

Thanks again,
Ronnie

Quote:

> Dim Y() As Double
> Dim Z() As Double

> For Counter = 0 To N - 1
>     ReDim Y(Counter) As Double
>     ReDim Z(Counter) As Double
>     Y(Counter) = radius * Cos(theta * (2 * Counter + 1))
>     Z(Counter) = radius * Sin(theta * (2 * Counter + 1))

>     format (Y(counter), "#.####")

>     corners.Text = corners.Text & ("(0, " & Y(Counter) & ", " & Z(Counter)
&
> ")") & vbCrLf

> Next Counter

> --
> Please reply to rwtam (at) engmail (dot) uwaterloo (dot) ca



Sat, 22 Sep 2001 03:00:00 GMT  
 Formatting/Rounding numbers - text box
Three points:

1) To answer your direct question, you need to use the Preserve Keyword in
order to preserve the previously assigned values.

2) The As Double you use on your Redim statements is optional. Since they
started out as Doubles and they remain as Doubles, you can omit the As
Double on the Redim.

3) Why are you Redim'ming inside of the loop. You already know how many
elements the arrays should be redim'med to -- N. Save your computer from
doing all that housekeeping and Redim only once, just before the loop
starts. Normally, including the Redim inside the loop is done when you don't
know how many times the loop will execute -- like in a Do-Loop.

Rick

Quote:

>Also, each time I go through the loop, the program seems to forget previous
>values saved to the array.  For example, the first time through the loop,
>Y(0) is given a value.  If I put a "print Y(0)" command in each time it
>loops through, the first time the correct value is displayed and then 0's
>are printed for each subsequent pass through the loop.

>What's wrong with my code?

>Thanks again,
>Ronnie

>> Dim Y() As Double
>> Dim Z() As Double

>> For Counter = 0 To N - 1
>>     ReDim Y(Counter) As Double
>>     ReDim Z(Counter) As Double
>>     Y(Counter) = radius * Cos(theta * (2 * Counter + 1))
>>     Z(Counter) = radius * Sin(theta * (2 * Counter + 1))

>>     format (Y(counter), "#.####")

>>     corners.Text = corners.Text & ("(0, " & Y(Counter) & ", " &
Z(Counter)
>&
>> ")") & vbCrLf

>> Next Counter

>> --
>> Please reply to rwtam (at) engmail (dot) uwaterloo (dot) ca



Sat, 22 Sep 2001 03:00:00 GMT  
 Formatting/Rounding numbers - text box
Look up the Format Statement in VB's Help. You want for your specific
question:

Format$(0.500001, "0.#") ==> 0.5
Format$(0.500001, "0.#0") ==> 0.50

Note: That numerical value (0.500001) can be a numerical/string
variable/expression.

Rick

Quote:

>I'm really a novice at Visual Basic.  Thanks to everyone who's helped me so
>far.  Here's another question!

>I'm having trouble rounding the results of my calculations which are
>displayed in a text box.  I'm trying to round the numbers so that the
>results are more easy on the eyes.  (ex:  0.5 --> shows up as
>0.500000000000001)

>Any suggestions on the proper syntax?

>Ronnie

>Dim Y() As Double
>Dim Z() As Double

>For Counter = 0 To N - 1
>    ReDim Y(Counter) As Double
>    ReDim Z(Counter) As Double
>    Y(Counter) = radius * Cos(theta * (2 * Counter + 1))
>    Z(Counter) = radius * Sin(theta * (2 * Counter + 1))

>    format (Y(counter), "#.####")

>    corners.Text = corners.Text & ("(0, " & Y(Counter) & ", " & Z(Counter)
&
>")") & vbCrLf

>Next Counter

>--
>Please reply to rwtam (at) engmail (dot) uwaterloo (dot) ca



Sat, 22 Sep 2001 03:00:00 GMT  
 Formatting/Rounding numbers - text box
I typed your code below exactly (just to see if it worked) but it wouldn't
accept my code.  I get an error message saying that  it expects a ")" where
the comma is.  Is it possible that the syntax below is for VB5 and above?
I'm using VB4.

Ronnie


Quote:
> Look up the Format Statement in VB's Help. You want for your specific
> question:

> Format$(0.500001, "0.#") ==> 0.5
> Format$(0.500001, "0.#0") ==> 0.50

> Note: That numerical value (0.500001) can be a numerical/string
> variable/expression.

> Rick




Quote:
> >I'm really a novice at Visual Basic.  Thanks to everyone who's helped me
so
> >far.  Here's another question!

> >I'm having trouble rounding the results of my calculations which are
> >displayed in a text box.  I'm trying to round the numbers so that the
> >results are more easy on the eyes.  (ex:  0.5 --> shows up as
> >0.500000000000001)

> >Any suggestions on the proper syntax?

> >Ronnie

> >Dim Y() As Double
> >Dim Z() As Double

> >For Counter = 0 To N - 1
> >    ReDim Y(Counter) As Double
> >    ReDim Z(Counter) As Double
> >    Y(Counter) = radius * Cos(theta * (2 * Counter + 1))
> >    Z(Counter) = radius * Sin(theta * (2 * Counter + 1))

> >    format (Y(counter), "#.####")

> >    corners.Text = corners.Text & ("(0, " & Y(Counter) & ", " &
Z(Counter)
> &
> >")") & vbCrLf

> >Next Counter

> >--
> >Please reply to rwtam (at) engmail (dot) uwaterloo (dot) ca



Sat, 22 Sep 2001 03:00:00 GMT  
 Formatting/Rounding numbers - text box

Quote:

>I typed your code below exactly (just to see if it worked) but it wouldn't
>accept my code.  I get an error message saying that  it expects a ")" where
>the comma is.  Is it possible that the syntax below is for VB5 and above?
>I'm using VB4.


>> Look up the Format Statement in VB's Help. You want for your specific
>> question:

>> Format$(0.500001, "0.#") ==> 0.5

That code is valid at least as far back as VB3.  The function needs to be
used as part of another statement to be useful:

corners.Text = corners.Text & "(0, " & Format$(Y(Counter),"0.##") & ", " & _
    Format$(Z(Counter),"0.##")  & ")" & vbCrLf



Sat, 22 Sep 2001 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Number Format in UserFrom Lables and Text Boxes

2. Text box data bound formatted number display

3. Formatting a Text Box for phone numbers

4. formatting numbers in text boxes

5. Number format in text box

6. Text Box Number Format

7. Format Numbers to Round up

8. Why Does Format(##,###.##) Round Numbers Up?

9. help: formatting rich text box/text box

10. Modifying Text Format in use without changing text in entire text box

11. formatting a text box to long date format

12. from Microsoft format Number to IEE format Number

 

 
Powered by phpBB® Forum Software