Re: VBA Code - Dividing Numbers

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Kevin K. Sullivan (reply_at_the.news.group)
Date: 10/25/04


Date: Mon, 25 Oct 2004 17:39:19 -0400

Go to the immediate pane and type
? 75/999
you'll see the result:
 7.50750750750751E-02
Note the last four characters -- E-02. Access is using scientific notation
to denote
7.507507... * 10 ^ -2
7.507507 * 0.01
0.07507507...

It's doing the math correctly, but you have an implicit conversion from
double to string that isn't cooperating with you.

Try:

' old code
'Print #1, dblVal & Chr(9) & x
'new code, uses comma to separate outputs with tab character

Print #1, dblVal, x

Another way is to use Format to make an explicit conversion from number to
string:

Print #1, Format(dblVal, "0.000000"), Format(x, "0")

This way will avoid scientific notation in all cases.

HTH,

Kevin

"Access101" <Access101@discussions.microsoft.com> wrote in message
news:FCBB8F66-F3FF-45F4-A0A1-00A452DC6B30@microsoft.com...
>
> I divide the number 75 by several numbers, however many of the resulting
> numbers have a misplaced decimal point. That is, 75 / 999 = 7.507
> (Incorrect) . But 75 / 1000 = .075 (Correct). And then 75/ 1001 = 7.492
>
> Here is the code. Any help is appreciated:
>
> Dim dblVal As Double, x as integer
> Open "C:\tmp.txt" For Output As #1
> For x = 999 To 2000
> dblVal = 75 / x
> Print #1, dblVal & Chr(9) & x
> Next x
> Close #1
>



Relevant Pages

  • NVarchar vs NText
    ... But this implicit conversion will fail if the string is ... greater than the maximum size of an NVarChar, ... For strings greater than 4000 characters, ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: How to convert Infix notation to postfix notation
    ... If this is for an error message, why isn't it using stderr for its output? ... array of 15 characters, and you call this function with the limit 15 on ... Making sure that the only string I allocate and append to, ... because mulFactor in all versions must needs incorporate the functions ...
    (comp.lang.c)
  • Re: Prothon should not borrow Python strings!
    ... """It does not make sense to have a string without knowing what encoding ... same cul de sac as Python. ... Prothon_String_As_ASCII // raises error if there are high characters ... Python's split between byte strings and Unicode strings is ...
    (comp.lang.python)
  • Re: Letter to US Sen. Byron Dorgan re unpaid overtime
    ... put them in stupid places. ... Programming is difficult (as you must surely appreciate, ... > strings will be in the range 1...1000 characters. ... impose an artificially small limit on string length." ...
    (comp.programming)
  • Re: Byte Array to String
    ... retrieved text will mismatch the original characters. ... encoding the characters. ... Dim strFileData as String ...
    (microsoft.public.dotnet.framework.aspnet)