Micro Focus QTP (UFT) Forums
how to check if the string's format is alphabetical or numeric - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: VB Scripting/Descriptive Programming (https://www.learnqtp.com/forums/Forum-VB-Scripting-Descriptive-Programming)
+--- Thread: how to check if the string's format is alphabetical or numeric (/Thread-how-to-check-if-the-string-s-format-is-alphabetical-or-numeric)



how to check if the string's format is alphabetical or numeric - lotos - 09-28-2010

hi all,
I have an issue with string formats.
As a result of some queries within DB I am getting or text values or numeric values assigned to my string, for e.g:
str_Value = Y
or
str_Value = 123

how can I check if "str_Value" is numeric or text format?!

The idea is that if it is a numeric format, I should change it's format:
Code:
str_Value = FormatNumber(str_Value, 2)
if not It will remain as it is within DB (text)


RE: how to check if the string's format is alphabetical or numeric - QTPLearn - 09-28-2010

Hi

Use 'IsNumeric' Function.It returns True if the expression is recognized as a number; otherwise, it returns False.

Ex:

Code:
If IsNumeric(str_Value ) Then

   str_Value = FormatNumber(str_Value, 2)
End If

~Regards


RE: how to check if the string's format is alphabetical or numeric - lotos - 09-28-2010

Hi QTPLearn,
thanks a lot, but what to do when:
Code:
str_Value = 123,231

in such cases this method doesn't work
I found a method which is good at the moment, but I am not sure if it will be good all the time(when the format of str_Value in the DB will be another and will be of a numbered format).
Code:
Dim MyCheck

MyCheck = varType(dBitem)
    If  MyCheck <> 8 Then
        ' formating the value of the string - max 2 nr.'s after the dot (e.g.: 123.09)
        dBitem = FormatNumber(dBitem, 2)
        Else
    End If

VarType Constants

These constants are only available when your project has an explicit reference to the appropriate type library containing these constant definitions. For VBScript, you must explicitly declare these constants in your code.

|Constant |Value| |Description|
vbEmpty |0 |Uninitialized (default)
vbNull |1 |Contains no valid data
vbInteger |2 |Integer subtype
vbLong |3 |Long subtype
vbSingle |4 |Single subtype
vbSingle |5 |Double subtype
vbCurrency |6 |Currency subtype
vbDate |7 |Date subtype
vbString |8 |String subtype
vbObject |9 |Object
vbError |10 |Error subtype
vbBoolean |11 |Boolean subtype
vbVariant |12 |Variant (used only for arrays of variants)
vbDataObject |13 |Data access object
vbDecimal |14 |Decimal subtype
vbByte |17 |Byte subtype
vbArray |8192 |Array

At the moment, my constants are of the vbDecimal type (nr. 14). But I am not sure that for all my tests/ queries this will be the only one type of constants.


RE: how to check if the string's format is alphabetical or numeric - cdesserich - 09-28-2010

IsNumeric() still works with "123,231". To use the FormatNumber() function, just explicitly convert it to a number with CLng() before formatting it back to a string:

Quote:str_Value = "123,231"

If IsNumeric(str_Value) Then
___str_Value = FormatNumber(CLng(str_Value), 2)
End If
MsgBox str_Value



RE: how to check if the string's format is alphabetical or numeric - lotos - 10-07-2010

Hi again, but what do you say if the number will have next format:
str_Value = 123,123.21? Shy


RE: how to check if the string's format is alphabetical or numeric - cdesserich - 10-07-2010

Just change "CLng()" to "CDbl()" It will work for both "123,123" and "123,123.21"

QTP Code Wrote:str_Value = "123,123.21"

If IsNumeric(str_Value) Then
___str_Value = FormatNumber(CDbl(str_Value), 2)
End If
MsgBox str_Value

Although, I would argue that "123,123.21" is already formatted the way you want it, so running it through the above code is really unnecessary, but if you don't know the format at runtime, I guess this approach should be fine.


RE: how to check if the string's format is alphabetical or numeric - lotos - 10-07-2010

thanks a lot.