Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在vb.net中,isnumeric无法将百分比字符串识别为数字_Vb.net - Fatal编程技术网

在vb.net中,isnumeric无法将百分比字符串识别为数字

在vb.net中,isnumeric无法将百分比字符串识别为数字,vb.net,Vb.net,我有一些自定义的双值字符串格式。我需要在某个时刻将这些字符串转换为双值(注意,这些字符串可能包含数字以外的错误消息)。所以我首先检查这些字符串是否是数字,如果是,然后将它们转换为双精度。Isnumeric适用于我的第一个自定义字符串格式。但我发现它是数字,无法识别百分比字符串。我想知道为什么isnumeric能理解()但不能理解%。还有什么更好的方法可以将“(100.00)”和“50%”转换为Cdbl以外的双倍 Private Format As String = "###,###,###,##

我有一些自定义的双值字符串格式。我需要在某个时刻将这些字符串转换为双值(注意,这些字符串可能包含数字以外的错误消息)。所以我首先检查这些字符串是否是数字,如果是,然后将它们转换为双精度。Isnumeric适用于我的第一个自定义字符串格式。但我发现它是数字,无法识别百分比字符串。我想知道为什么isnumeric能理解()但不能理解%。还有什么更好的方法可以将“(100.00)”和“50%”转换为Cdbl以外的双倍

Private Format As String = "###,###,###,##0.00;(###,###,###,##0.00);0" 'negative values are in the format (number)
Private rateFormat as string="p"
Dim str1 as string=-100.0.Tostring(Format)  'str1=(100.0)
Dim str2 as string=0.5.Tostring(rateFormat) 'str2=50%
Dim value1,value2 as double
If isnumeric(str1)
value1=Cdbl(str1) 'True. value1=-100.0
else
value1=Double.MinValue
End If
If isnumeric(str2)
value2=cdbl(str2) 'False.value2=Double.Minvalue
else
value2=Double.MinValue
End If

isnumeric返回一个布尔值,指示表达式是否可以作为数字计算。在您的情况下,%不是一个数字,因此错误。请尝试以下代码

If isnumeric(str2.Substring(0,str2.length-2))
如果表达式的数据类型为布尔型、字节型、十进制型、双精度型、整数型、长型、SByte型、短型、单精度型、UInteger型、ULong型或UShort型,或者包含其中一种数值类型的对象,则IsNumeric返回True。如果表达式是可以成功转换为数字的字符或字符串,它也会返回True

如果表达式为数据类型Date或数据类型Object且不包含数字类型,则IsNumeric返回False。如果表达式是无法转换为数字的字符或字符串,则IsNumeric返回False。

()实际上是数字格式。它在簿记中用来表示负值,而不是“-”


请参阅,或者首先,
是数字的
C
通常不使用,因为它们可以提供意外的输出。通常您会使用
Double.TryParse
Convert.ToDouble
(或任何类型),因为它们可以像您期望的那样可靠地解析数字;你只需要确保它的格式正确

此外,这些方法更有效,因为您可以确定字符串是否是数字,并一次性解析所有字符串。请注意,
Parse
Convert
在格式错误时抛出异常

现在,据我所知,有一种内置的从百分比符号转换的方法。我继续写了三种方法(当前区域性、任意区域性、不变区域性),在百分比符号的四个可能位置上测试为0.5和-0.5

'these will return Double.NaN if they fails
'this parses based on the current culture
Public Function ParseDoublePercent(ByVal input As String) As Double
    Return ParseDoublePercent(input, System.Globalization.NumberFormatInfo.CurrentInfo)
End Function

'invariant culture
Public Function ParseDoublePercentInvariant(ByVal input As String) As Double
    Return ParseDoublePercent(input, System.Globalization.NumberFormatInfo.InvariantInfo)
End Function

'this parses based on a specified culture
Public Function ParseDoublePercent(ByVal input As String, ByVal provider As IFormatProvider) As Double
    If String.IsNullOrEmpty(input) OrElse input.Length < 3 Then Return Double.NaN 'minimum length of string is 2 (0%)
    'get some information about how the percent should be formatted
    Dim format As System.Globalization.NumberFormatInfo = System.Globalization.NumberFormatInfo.GetInstance(provider)

    'how this will be parsed will first depend on if it's positive or negative
    Dim isNegative As Boolean = input.Substring(0, 1) = "-"
    Dim percentPattern As Integer = If(isNegative, format.PercentNegativePattern, format.PercentPositivePattern)

    'i'm using convert.todouble because I don't want to use NumberStyles in Double.TryParse
    Try
        Select Case percentPattern
            Case 0 'n %, such as -50.00 %, 50.00 %
                Return Convert.ToDouble(
                    input.Replace(" " & format.PercentSymbol, "e-" & format.PercentDecimalDigits),
                    provider)
            Case 1 'n%, such as -50.00%, 50.00%
                Return Convert.ToDouble(
                    input.Replace(format.PercentSymbol, "e-" & format.PercentDecimalDigits),
                    provider)
            Case 2 '%n, such as -%50.00, %50.00
                'need to do some special parsing just in case there are incorrect percent signs in the middle of the number
                'dont want to just replace all of them
                Return Convert.ToDouble(
                        input.Remove(If(isNegative, 1, 0), 1) & "e-" & format.PercentDecimalDigits, provider)
            Case 3 '% n, such as '%-50.00, % 50.00
                Return Convert.ToDouble(input.Remove(0, 1) & "e-" & format.PercentDecimalDigits, provider)
            Case Else
                Return Double.NaN 'should not happen
        End Select
    Catch ex As FormatException 'an exception is thrown when the string fails to parse
        Return Double.NaN
    End Try
End Function
“如果失败,将返回Double.NaN
'这将基于当前区域性进行分析
公共函数将DoublePercent(ByVal输入为字符串)解析为Double
返回ParseDoublePercent(输入,System.Globalization.NumberFormatInfo.CurrentInfo)
端函数
“不变文化”
公共函数将DoublePercent不变量(ByVal输入作为字符串)解析为Double
返回ParseDoublePercent(输入,System.Globalization.NumberFormatInfo.InvariantInfo)
端函数
'这将基于指定的区域性进行分析
公共函数将DoublePercent(ByVal输入作为字符串,ByVal提供程序作为IFormatProvider)解析为Double
如果String.IsNullOrEmpty(input)或lse input.Length<3,则返回Double.NaN'字符串的最小长度为2(0%)
'获取有关百分比应如何格式化的信息
Dim格式为System.Globalization.NumberFormatInfo=System.Globalization.NumberFormatInfo.GetInstance(提供程序)
“这将如何解析将首先取决于它是正的还是负的
Dim为负,布尔值=输入。子字符串(0,1)=“-”
Dim percentPattern As Integer=If(为负,格式为.PercentNegativePattern,格式为.PercentPositivePattern)
'我正在使用convert.todouble,因为我不想在Double.TryParse中使用numberstyle
尝试
选择案例百分比模式
案例0'n%,例如-50.00%,50.00%
返回Convert.ToDouble(
输入.替换(“&format.PercentSymbol,&e-”&format.PercentDecimalDigits),
供应商)
案例1’n%,例如-50.00%,50.00%
返回Convert.ToDouble(
输入.替换(format.PercentSymbol,“e-”和format.PercentDecimalDigits),
供应商)
案例2“%n”,例如-%50.00,%50.00
需要做一些特殊的解析,以防在数字中间有不正确的百分率符号。
“我不想把它们全部替换掉
返回Convert.ToDouble(
input.Remove(如果(isNegative,1,0),1)和“e-”&format.PercentDecimalDigits,provider)
案例3“%n”,例如“%-50.00,%50.00
返回Convert.ToDouble(input.Remove(0,1)&“e-”&format.PercentDecimalDigits,provider)
其他情况
不应出现“Return Double.NaN”
结束选择
Catch ex As FormatException“当字符串解析失败时引发异常
返回Double.NaN
结束尝试
端函数

事实上,这是一种数字格式。在您的情况下,%不是数字,因此错误。

(100.0)也不是数字。但是为什么isNumeric知道它是负值并将其转换为双精度-100.0.100.0是双精度值,因此是数值。谢谢你的回答。对于负值,字符串格式为(数字)。所以字符串是“(100.0)”。我想知道为什么isnumeric可以理解()但不能理解%.在我给出答案之前,您的代码是全局使用还是仅与一个
CultureInfo
一起使用?如果只有一个,你住在哪个国家?我住在加拿大。我将使用cultureInfo.InvariantCulture。似乎将百分比字符串解析为数字需要替换%[link]嗯,我很无聊,所以我想我应该开始编写一些适用于所有文化的代码。如果你现在需要代码,我很乐意为你写一些有用的代码。我非常感谢你的帮助!