Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number of digits after decimal point 2

Status
Not open for further replies.

Andrzejek

Programmer
Jan 10, 2006
8,569
US
Hi Gang,

Google search gave me nothing, so here it goes:

Is there a better way to find out how many digits there are after decimal point in the given number?

123 => 0
12.56 => 2
3.4 => 1 etc.

Right now I have a little Function that accepts a number and returns another number (of digits after decimal point + 1, this 1 is the decimal point itself)

Private Function MoveNumberBy(sngValue As Single) As Double

If InStr(1, CStr(sngValue), ".") > 0 Then
MoveNumberBy = (Len(CStr(sngValue)) - InStr(1, CStr(sngValue), ".")) + 1

End If

End Function

I hope there is a better way than dealing with converting the number to string and messing with it like that.

---- Andy
 
>Is there a particular reason

Legacy reason. This isn't production code, so it hasn't been cleaned up. In the earliest version I was intending to use LSet (which I use quite a lot to mangle variables). However I'd forgotten that if a UDT contains a VAriant then you can't use LSet, so I converted to using CopyMemory but didn't bother to eliminate the type I was using (since it didn't matter).
 
Excellent, strongm. :)

One thing wasn't clear to me right away, so I'll explain it in case it helps someone else.

The amount of memory that CopyMemory copies is determined by the data type of the source and destination arguments. (You have to be careful not to use CopyMemory with a larger source type than the destination type will hold.) So, strongm's code has a variant, which is 16 bytes (usually; variants containing strings have more and are variable in size) as the source, and an array of 16 members of type byte as the destination. So, source and destination are the same size. When you use CopyMemory, the first byte of the source will go into the first array element of the destination, the second will go to the second, and so on.

The point about the destination is that you can evaluate each byte of the array individually in the VB context, which you can't do with the source. So the effect of strongm's code is to break up the source into its individual bytes so you can look at them in VB.

HTH

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top