Hi Dave,
You could do the round( val, digits) to get the number of places.
then do the string conversion as I said.
Then search for the local string either , or . and take all values from it's
right side.
Once that string is taken, then just convert to your choice of value, int, dbl,
float and so on...
But, if you just want those to the right, you already have it in the new string
made.
I think there is a local function in VB to do the character search you want. If
you have decimals, how can a comma get in the number?
Anyway, I think I have given you the tools, a simple 3 line function, maybe
4 lines and you are ready to go.
strDecimals = Replace( strDbl, left( strDbl, InStrRev( strDbl, ".")), "")
Now the above has taken the first local character for a decimal seen on the
right side and eliminated everything up to it. The remainder is everyting to
the right of that decimal point, or local string you choose.
Bruce
Sent: Tuesday, January 01, 2013 2:05 PM
Subject: Re: VBS code - floating point numbers
Bruce,
Thanks for your attempt to help. Sorry, but your sample code has two
drawbacks - hence won't do the trick.
First of all, you have locked the decimal point character, to always be the
period (or dot) sign - in your Replace instruction. Since the script is going
to accept input from the user, and the user could have chosen another character
for the decimal point - through his locale settings - I cannot lock the
character in a Replace instruction. Unless, there would happen to be an easy
way to determine what character the computer is using for a decimal point, at
any given time. May very likely be new to you, but if I switch from English US
keyboard layout on my computer, and make the Keyboard Layout any of the
Scandinavian ones for instance, even the Del-key on the numeric keypad, will no
longer give a period when numlock is on. So, if I set the keyboard to Danish
for instance, and enter a number like 123.45 on the numeric keypad, the number
that is actually being entered (and which the script will see), is 123,45. Just
to illustrate that changing the locale setting of the computer, will result in
the numbers being differently entered, even with the same keys. You make an
Excel spreadsheet under English US Locale, entering some decimal numbers, they
all will show up with the Period as a decimal point. Keep the same spreadsheet
unaltered, but change your Locale settings to another language, and your Excel
spreadsheet will immediately have all its numbers displaying with commas as the
decimal point. And, at least under Windows XP, I saw a place where you could
choose which character should be used for your decimal point, under the Locale
settings.
Secondly, and maybe the easiest to show you, is that your sample code, simply
makes one big number, from the original number. Maybe I wasn't all clear on
what I want to accomplish, and so you must have misunderstood the target of the
issue. What your sample code does, is to change the number
123.4567890
, into
1234567890
. But that is not what I want. Smile.
Here is a few examples. Below, on each line, I will enter two numbers. The
first one, is the number that the script will be receiving through any kind of
input, the second number on the line, is what I expect for a return. Hopefully,
this will clarify what I am after.
3.42 = 42
3.141592 = 141592
123.4567890 = 4567890
5.934 = 934
6.7812 = 7812
.
Thanks again, for giving it a try.
----- Original Message -----
From: BX
To: [email protected]
Sent: Tuesday, January 01, 2013 4:45 PM
Subject: Re: VBS code - floating point numbers
Hi Dave,
Try this simple conversion method and see if it works!
Bruce
Example Dim sdbl, dblValue
dblValue = 123.4567890
sdbl = CStr( dblValue)
Replace( sdbl, ".", "")
dblValue = CDbl( sdbl)
Sent: Tuesday, January 01, 2013 10:13 AM
Subject: Re: VBS code - floating point numbers
Thanks Bruce, for your feedback.
Now, the script will receive inputs, and it could never be told how many
decimals will be in the incoming number. The number I gave, was just to
illustrate. One time, you will get the number 3.42, next time 5.4495; and
another time 123.9087654.
The ideas you outlined, will they work? Of course, I could use your
approach (* 1000000.0), but that I guess, would not work if there is only 3 or
4 decimals. And, what if you get a number with 10 decimals. I then would have
to come up with a way to determine how many decimals are in the incoming
number, before I could take your approach - wouldn't I? Or, am I missing your
point.
----- Original Message -----
From: BX
To: [email protected]
Sent: Tuesday, January 01, 2013 3:57 PM
Subject: Re: VBS code - floating point numbers
Hi Dave,
You just use the decimal factor, if you want to move it 3 places
the use times 1000.
Now to keep things in the same format make sure use use the decimal
point all the time. Even though VB does not care, too loose in it's numbering
systems.
123.4567890 * 1000000.0
Or a shift left would also do the same thing...
Bruce
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.