简体   繁体   English

在Windows Mobile上的VB.NET中键入convertion

[英]Type convertion in VB.NET on Windows Mobile

I am maintaining an application for a Barcode-scanner running Windows Mobile with .NET 2.0 SP 1 and VB.NET. 我正在维护运行带有.NET 2.0 SP 1和VB.NET的Windows Mobile的条形码扫描程序的应用程序。 The application integrates with an Oracle database using PL/SQL. 该应用程序使用PL / SQL与Oracle数据库集成。

I have a SQL-query that returns a DataTable with list of items to be picked with the scanner, and sometimes the quantity is a decimal number. 我有一个SQL查询返回一个DataTable,其中包含要使用扫描程序选择的项目列表,有时数量是十进制数字。

The problem is that I can't convert a decimal number such as 0,8 from string to double. 问题是我无法将0,8等十进制数字从字符串转换为double。 I have tried all of the following: 我尝试了以下所有方法:

Dim quantity As String = "0,8"
Dim result As Double = Convert.ToDouble(quantity)
Dim result As Double = Double.Parse(quantity)
Dim result As Double = CDbl(quantity)

In all cases the result is 8 and not 0,8. 在所有情况下,结果是8而不是0.8。

The CF is limited in it's ability to parse. CF的解析能力有限。 I assume the device culture is set to en-US, in which case you could do something like this: 我假设设备文化设置为en-US,在这种情况下,您可以执行以下操作:

Dim quantity As String = "0,8"
Dim result As Double = Double.Parse(quantity.Replace(',', '.'))

If you wanted it to be more locale-aware as far as the device's current setting, you could be more safe with something like this: 如果你希望它对设备的当前设置更具区域设置意识,那么你可能会更加安全:

Dim quantity As String = "0,8"
Dim result As Double

If CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator == "."
    result = Double.Parse(quantity.Replace(',', '.'))
Else
    result = Double.Parse(quantity)
End If

Looks like your CurrentCulture has . 看起来像你的CurrentCulture . as decimal separator and you're trying to parse value with , instead. 作为小数分隔符,你试图解析值,而不是。

Try that: 试试看:

System.Threading.Thread.Current.CurrentCulture.NumberFormat.NumberDecimalSeparator = ","
Dim result As Double = Double.Parse(quantity)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM