Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to check if the string's format is alphabetical or numeric
#1
Solved: 11 Years, 3 Months, 4 Weeks ago Exclamation 
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)
Reply
#2
Solved: 11 Years, 3 Months, 4 Weeks ago
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
Reply
#3
Solved: 11 Years, 3 Months, 4 Weeks ago
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.
Reply
#4
Solved: 11 Years, 3 Months, 4 Weeks ago
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
Reply
#5
Solved: 11 Years, 3 Months, 4 Weeks ago
Hi again, but what do you say if the number will have next format:
str_Value = 123,123.21? Shy
Reply
#6
Solved: 11 Years, 3 Months, 4 Weeks ago
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.
Reply
#7
Solved: 11 Years, 3 Months, 4 Weeks ago
thanks a lot.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to change 24 hrs time format to 12 hrs time format Varsha Gupta 1 2,831 04-21-2018, 11:06 AM
Last Post: Ankur
  How to validate a format of an alphanumeric string PrateepKaraval 2 5,995 11-08-2014, 10:26 AM
Last Post: vinod123
  Unable to read numeric values from WebTable into Datatable Akhila 2 4,717 11-26-2013, 03:43 PM
Last Post: Akhila
  Pdf to Excel or any other Format Conversion branjitk 1 2,475 06-14-2012, 08:14 AM
Last Post: basanth27
  How to extract numeric values hamzaz 3 3,868 04-26-2012, 03:15 PM
Last Post: ssvali

Forum Jump:


Users browsing this thread: 1 Guest(s)