解析包含逗号或句点的数字

时间:2016-05-18 13:37:06

标签: vb.net

我有三个值需要从最高值到最低值排序。我使用下面的代码就像一个魅力,直到我想使用句号"。"和逗号","。如果我输入" 1,3"它显示我喜欢,但如果我输入" 1.3"它更改为13.我的最终用户需要能够使用逗号和句点。

我该如何解决这个问题?

    Dim IntArr(2) As Decimal
    IntArr(0) = TextBox1.Text
    IntArr(1) = TextBox2.Text
    IntArr(2) = TextBox3.Text
    Array.Sort(IntArr)

    Dim highestNum As Decimal
    Dim Midelnum As Decimal
    Dim lowestNum As Decimal

    lowestNum = IntArr(0)
    Midelnum = IntArr(1)
    highestNum = IntArr(2)

    MsgBox("Highest " & highestNum)
    MsgBox("lowest " & lowestNum)
    MsgBox("middel " & Midelnum)

3 个答案:

答案 0 :(得分:0)

你可以在保存字符串之前使用改变字符串的方法:

  TextBox.Text.Replace(".",",")

但是如果你想显示原始输入,你可以有一个变量来检测输入的字符:

  Dim isDot As Boolean = False
   Dim number As String = TextBox.Text

   If number.Contains(".") Then
       isDot = True
   End If

最后只是为了显示

而替换它
  If isDot Then
       number.Replace(",",".")
   End If

答案 1 :(得分:0)

问题在于它是基于文化的。我之所以这么说,是因为如果我按照你的描述输入数字,我会得到相反的效果(" 1,3" - >" 13"等)。

这是一种快速更改值以匹配当前文化的方法。

在课堂上,请输入:

Imports System.Globalization

然后,你可以这样做:

  Dim IntArr(2) As Decimal

  Dim nfi As NumberFormatInfo = CultureInfo.CurrentCulture.NumberFormat
  Dim sep1 As String = nfi.NumberDecimalSeparator
  Dim sep2 As String = If(sep1.Equals("."), ",", ".")
  Dim useComma As Boolean = (TextBox1.Text.Contains(",") Or TextBox2.Text.Contains(",") Or TextBox3.Text.Contains(","))

  'Replace the separator to match the current culture for parsing
  Decimal.TryParse(TextBox1.Text.Replace(sep2, sep1), IntArr(0))
  Decimal.TryParse(TextBox2.Text.Replace(sep2, sep1), IntArr(1))
  Decimal.TryParse(TextBox3.Text.Replace(sep2, sep1), IntArr(2))

  Array.Sort(IntArr)

  sep1 = If(useComma, ",", ".")
  sep2 = If(useComma, ".", ",")

  'Reformat the results to match the user's input
  Dim lowestNum As String = IntArr(0).ToString().Replace(sep2, sep1)
  Dim middleNum As String = IntArr(1).ToString().Replace(sep2, sep1)
  Dim highestNum As String = IntArr(2).ToString().Replace(sep2, sep1)

  Dim msg As String = "Highest: {0}" & Environment.NewLine & _
                      "Lowest: {1}" & Environment.NewLine & _
                      "Middle: {2}"

  msg = String.Format(msg, highestNum, lowestNum, middleNum)

  MessageBox.Show(msg)

此外,由于您使用的是.NET,因此您可能希望跳过VB6的处理方式。请参阅我的示例,了解我使用过的内容。

答案 2 :(得分:0)

接受的答案使用了太多不必要的字符串操作。您可以使用CultureInfo对象来获取所需内容:

Sub Main
    Dim DecArr(2) As Decimal

    'Select the input culture (German in this case)
    Dim inputCulture As CultureInfo = CultureInfo.GetCultureInfo("de-DE")

    Dim text1 As String = "1,2"
    Dim text2 As String = "5,8"
    Dim text3 As String = "4,567"


    'Use the input culture to parse the strings.
    'Side Note:  It is best practice to check the return value from TryParse
    '            to make sure the parsing actually succeeded.
    Decimal.TryParse(text1, NumberStyles.Number, inputCulture, DecArr(0))
    Decimal.TryParse(text2, NumberStyles.Number, inputCulture, DecArr(1))
    Decimal.TryParse(text3, NumberStyles.Number, inputCulture, DecArr(2))

    Array.Sort(DecArr)

    Dim format As String = "Highest: {0}" & Environment.NewLine & _
                            "Lowest: {1}" & Environment.NewLine & _
                            "Middle: {2}"

    'Select the output culture (US english in this case)
    Dim ouputCulture As CultureInfo = CultureInfo.GetCultureInfo("en-US")

    Dim msg As String = String.Format(ouputCulture, format, DecArr(2), DecArr(1), DecArr(0))

    Console.WriteLine(msg)
End Sub

此代码输出:

Highest: 5.8
Lowest: 4.567
Middle: 1.2