DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Convert a Variable to only 4 digits

Solved!
Go to solution

Hello:

I have the following code:

dim  Var
Var=CDbl(Data.Root.ChannelGroups(1).Properties("Velocity_test_object_1").Value)


But the velocity test, is filled by another program before creating the TDM. (and somehow it does not recognize decimal points.)
I want only the first 4 digits as var.

 

For example:
Velocity=32345   Var=3234
Velocity=23         Var=2300

Velocity=1501     Var=1501

Velocity=555555 Var=5555

 

 

Either that or a way to read decimal points! 😛

Can someone help

 

 

0 Kudos
Message 1 of 4
(2,660 Views)
Solution
Accepted by topic author LP_C

CDbl is local dependent.

I assume your values have an other decimal value which is interpreted as thousand seperator.

 

You can use Val instead which always uses dot as decimal point.

MsgBox Val("1.2") & VBCRLF & Val("1,2") & VBCRLF & CDbl("1.2")& VBCRLF & CDbl("1,2")

So potentially

dim  Var
Var=Val(Data.Root.ChannelGroups(1).Properties("Velocity_test_object_1").Value)

might do the job.

 

 

 

Message 2 of 4
(2,648 Views)
' Variables
Dim velocity: velocity = Array(32345, 23, 1501, 555555)
Dim desiredDigits: desiredDigits = 4

' Conversion loop
Dim i, number, length
For i = 0 To UBound(velocity, 1) Step 1 ' For each velocity case
  length = Len(CStr(velocity(i))) ' get the number of digits
  If length >= desiredDigits Then ' If number of digits is bigger or equal desired
    number = Left(CStr(velocity(i)), desiredDigits) ' Simply take the leftmost digits
  Else
    number = Left(CStr(velocity(i)), length) ' Otherwise take all the leftmost digits (kinda redundant)
    While Len(number) < desiredDigits ' And keep adding trailing zeroes until reached desired number of digits
      number = number & "0"
    Wend
  End If
  Call LogFileWrite(number) ' Here number is a "String" type so simple convert CDbl(number) if you need
Next

 You can change the values in the "velocity" array to add cases or change the number of desired output digits in the variables at the top.

 

Output:

3224

2300

1501

5555

0 Kudos
Message 3 of 4
(2,624 Views)

Second method did the trick

0 Kudos
Message 4 of 4
(2,548 Views)