Hi David,

 

Doug has given you the answer, but just to try to add more to it: binary
floating point numbers almost never are stored in a form which exactly
equals a string you can type in; even when you enter 123.456789 into a
variable, the contents of this variable are not stored so that they can be
relied on to exactly equal 123.456789.  Sometimes it will, sometimes it
won't.  This is why, the results of most decimal calculations are rounded to
some expected number of decimal digits to the right of the decimal point
when displayed.

 

If you need this decimal value as a result of working with user input, or
something stored in a database, as Doug says, the only way you can rely on
reproducing what the user entered is to store the information as a string,
or, make use of the round function to display a known (and limited) number
of characters to the right of the decimal point.

 

I come up against this in the routines where I work with latitude and
longitude coordinates stored in decimal degree formats.  I want to do
numeric calculations on them, so whenever I show them to the user I have to
use the round function to limit the number of characters shown.

 

Other programming languages do offer binary decimal formats which exactly
represent a defined number of decimals (and VBScript does when you use the
currency type), but VBScript doesn't offer this; just the binary floating
point.

 

Hth,

 

Chip

 

 

 

 

From: David [mailto:[email protected]] 
Sent: Tuesday, January 01, 2013 9:27 AM
To: [email protected]
Subject: VBS code - floating point numbers

 

I have been playing for a while now, with a tricky numeric issue. Hopefully,
it is just that I have overlooked some kind of instruction. What I am trying
to do, is to derive the decimals, from a floating point number.

 

Let's for instance say, you have the number:

    123.4567890

I know, you can use the Int Or CInt instructions on this number, and it will
return 123. That is, the digits to the left of the decimal point. But what I
am after, is a way to have returned the digits to the right of the decimal
point - in the example above, that would give 4567890. I really can't seem
to find an instruction that will let me do this. Or, maybe it is named
somthing, that my English knowledge would not have included. Smile. So if
anyone out there, would happen to know of such an instruction or workarounds
for this task, I would greatly appreciate your feedback.

 

OK, You would think, that if we did a basic piece of math, things should not
get too complicated. So I thought, if I take my original number, and
subtract the Int value of the number, I would end up with a simple decimal
number, that I then could do some extra work on. Well, I tried a code like
this:

X = 123.4567890

speak x -int(x)

. What I did expect, was to get the return value of "0.4567890". But, that's
not what I get. My script will speak out the number of

    0.456789000000001

. and if you do a more direct way, like:

    speak 123.4567890 -123

; you might end up with an even less predictable number.

 

Well, my idea was - for a workaround - to have converted the returned value
into a string, and then simply omitted the first two characters (which would
be 0.). Then, I could have converted the final string back to a number, and
had the job done. Not exactly anything straight forward, but it would have
been a workaround. Yet, long as the returned value is not as expected, that
workaround would not be useful.

 

Again, is there an instruction in VBS, that will directly return only the
digits to the right of the decimal point?

 

One more thing. Some of you, might be wondering, why don't I just convert
the original number into a string, and then split by the decimal point. A
code like this:

    x = 123.4567890

    NumString = Split(X, ".")

    Speak NumString(1)

. OK, this would work perfectly; long as we are operating numbers that
follow English standard, with the dot-sign as the decimal point. But in
other languages, the comma-sign is used as a decimal point. Since I want the
script to work, no matter the locale setting of the computer, and the
corresponding decimal point character, I would have been more satisfied with
a direct instruction for deriving the digits to the right of the decimal
point.

 

Hope all of this makes sense, and that someone could give me a kick in the
right direction for a solution.

 

Reply via email to