检测数的文化在VB.Net即句号或逗号小数点/千位分隔符 [英] Detect culture of number in VB.Net i.e. period or comma for decimal point / thousands separator

查看:626
本文介绍了检测数的文化在VB.Net即句号或逗号小数点/千位分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VB.Net,是否有自动检测线重新一批presentation文化的一种方式?我会解释的情况:

In VB.Net, is there a way of auto-detecting the culture of a string representation of a number? I'll explain the situation:

我们的asp.net网站接收船数据的XML数据源。在大多数情况下,对价格的数字格式使用一个简单的非格式化的整数例如999000。这很容易让我们来处理。

Our asp.net web site receives xml data feeds for boat data. Most of the time, the number format for the prices use either a simple non-formatted integer e.g. "999000". That's easy for us to process.

Occaisionally,也有千位分隔符和时期的小数点逗号。此外,由于我们的数据导入明白这一点这很好。例如999,000.00。

Occaisionally, there are commas for thousands separators and periods for the decimal point. Also, that's fine as our data import understands this. Example "999,000.00".

我们已经开始从法国有些地方的房价已经进入的时期,数千得到一些数据分隔符周围的其他方法因为这是它在许多欧洲国家所做的那样。例如。 999.000,00。这是我们的系统会间preT,随着999磅,而不是被预期的999000英镑。

We're starting to get some data from France where some of the prices have been entered with the periods and thousands separators the other way around as that's the way it's done in many European countries. E.g. "999.000,00". This is where our system would interpret that as nine hundred and ninety nine pounds instead of the nine hundred and ninety nine thousand pounds that was intended.

不幸的是,数据馈送包含的格式的混合价格不会对每一个任何文化指标。有谁知道任何内置.NET函数,将自动检测的基础上,其中期和逗号是一个串号的文化?

Unfortunately, the data feed contains prices in a mixture of the formats without any culture indicator on each one. Does anyone know of any in-built .net functions that will auto-detect the culture of a string number based on where the period and comma are?

推荐答案

有没有内置的方式来从数字字符串确定CultureInfo的,据我所知。我严重怀疑它会永远是,因为没有100%安全的方式来做到这一点。

There is no built-in way to determine the CultureInfo from a numeric string, as far as I know. And I seriously doubt it'll ever be, because there is no 100% safe way to do it.

直到你找到一个更好的解决方案(例如:在发送端的一些改变),我想你能做的最好是减少两个步骤出错的可能性:

Until you find a better solution (eg: some change on the sender-side), I guess the best you can do is to decrease the chances of error in two steps:

1)输入数据清理和规范

Dim input as String = " 99 9.000,00 "
' This way you can remove unwanted characters (anything that is not a digit, and the following symbols: ".", "-", ",")
Dim fixedInput as String = Regex.Replace(input, "[^\d-,\.]", "")
' fixedInput now is "999.000,00"

2)怀疑自己的格式

Dim indexOfDot as Integer = fixedInput.IndexOf(".")
Dim indexOfComma as Integer = fixedInput.IndexOf(",")
Dim cultureTestOrder as List(Of CultureInfo) = new List(Of CultureInfo)
Dim parsingResult as Double?
Try
    If indexOfDot > 0 And indexOfComma > 0 Then
        ' There are both the dot and the comma..let's check their order
        If indexOfDot > indexOfComma Then
            ' The dot comes after the comma. It should be en-US like Culture
            parsingResult = Double.Parse(fixedInput, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"))
        Else
            ' The dot comes after the comma. It should be it-IT like Culture
            parsingResult = Double.Parse(fixedInput, NumberStyles.Number, CultureInfo.GetCultureInfo("it-IT"))
        End If
    Else If indexOfDot = fixedInput.Length-3 Then
        ' There is only the dot! And it is followed by exactly two digits..it should be en-US like Culture
        parsingResult = Double.Parse(fixedInput, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"))
    Else If indexOfComma = fixedInput.Length-3 Then
        ' There is only the comma! And it is followed by exactly two digits..it should be en-US like Culture
        parsingResult = Double.Parse(fixedInput, NumberStyles.Number, CultureInfo.GetCultureInfo("it-IT"))
    End If
Catch
End Try
If Not parsingResult.HasValue Then
    Try
        ' There is no dot or comma, or the parsing failed for some reason. Let's try a less specific parsing.
        parsingResult = Double.Parse(fixedInput, NumberStyles.Any, NumberFormatInfo.InvariantInfo)
    Catch
    End Try 
End If
If Not parsingResult.HasValue Then
    ' Conversion not possible, throw exception or do something else
Else
    ' Use parsingResult.Value
End If

您不是100%安全的这种方式,但它应该是比你目前的code得更好(至少在示例数据按预期工作你提供)。

You are not 100% safe this way, but it should be still better than your current code (and at least works as expected on the example data your provided).

这篇关于检测数的文化在VB.Net即句号或逗号小数点/千位分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆