在UFT / QTP中将字符串转换为double

时间:2017-09-21 13:39:20

标签: hp-uft

我试图在UFT中将字符串转换为double但它显示没有小数点的输出。以下是供参考的代码。

vStr = "1000000.589765"
msgbox Typename(vStr)
strV1=CDBL(formatNumber(vStr,4))
msgbox Typename(strV1)
print strV1

输出:1000000589765

请注意,如果没有formatNumber,则无效。

1 个答案:

答案 0 :(得分:0)

使用DotNetFactory的又一个实现。只是另一个想法。我不否认使用 CDbl 。但值得一试。

'Test Code
Dim strConvertedCode
strConvertedCode = ConvertDataType("1000000.589765","Double")
If strConvertedCode <> null Then
    Msgbox strConvertedCode 
End If

Public Function ConvertDataType(ByVal SourceData,ByVal ConversionDataType)
    'Initialization of variables 
    Dim objDotNetFactory
    Dim strConvertedData : strConvertedData = null
    Dim strSystemNamespace
    'Determine the destination data type
    Select Case UCase(ConversionDataType)
        Case "DOUBLE"   
            strSystemNamespace = "System.Double"
        'Implement further for your data types
        'Reference https://msdn.microsoft.com/en-us/library/ms228360(v=vs.90).aspx
        Case Default
            Set objDotNetFactory = DotNetFactory.CreateInstance("System.Int32")
    End Select
    Set objDotNetFactory = DotNetFactory.CreateInstance(strSystemNamespace)
    'Check the dot net factory instance is successful
    If Not IsObject(objDotNetFactory) Then
        Reporter.ReportEvent micWarning,"Data type convertor","Conversion from String to " & ConversionDataType & " failed, Since DotNetFactory instance was not created."
        ConvertDataType = strConvertedData
        Exit Function
    End If
    strConvertedData = objDotNetFactory.Parse(SourceData)
    ConvertDataType = strConvertedData
End Function