首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >tryParse不转换值

tryParse不转换值
EN

Stack Overflow用户
提问于 2013-06-24 09:15:07
回答 2查看 433关注 0票数 0

我有严格的选择权。当我打开它时,下面的代码不起作用。我能够将问题隔离到下面这一行

代码语言:javascript
复制
Decimal.TryParse(lblAnnualMid.Text, decAnnualMid)

由于此行没有将值从文本更改为小数,因此我的其余代码不起作用,并且我的标签显示$0。

我怎么才能解决这个问题。我仍然在VB.Net中摸索,所以如果这看起来很明显,请原谅我。

下面是我的代码的其余部分:

代码语言:javascript
复制
Option Strict On
Option Explicit On

Public Class frmCustomRanges

    'Variables for the MidPoint TextBoxes
    Dim decAnnualMid As Decimal
    Dim decHourlyMid As Decimal

Private Sub txtRangeSpread_TextChanged(sender As Object, e As EventArgs) Handles txtRangeSpread.TextChanged

        'This event runs when the user enters a number in the 
        'txtRangeSpread text box. The amount is converted to a decimal
        'it then calculates the min and max of the range for annual
        'and hourly ranges.  The ranges change as the user changes the range
        'spread.


        'Variables for the event
        Dim decRangeSpreadResults As Decimal

        'Verify entry is numeric
        If IsNumeric(txtRangeSpread.Text) Then

            'Convert entry to decimal
            Dim decRangeSpread As Decimal = Convert.ToDecimal(txtRangeSpread.Text)

            'convert the Mid point value to decimal
            Decimal.TryParse(lblAnnualMid.Text, decAnnualMid)

            'convert range spread value to percentage
            decRangeSpreadResults = decRangeSpread / 100


            'Display results in dollar string Annual salary
            lblAnnualMin.Text = Convert.ToDecimal(decAnnualMid - (decRangeSpreadResults * decAnnualMid)).ToString("C")
            lblAnnualMax.Text = Convert.ToDecimal(decAnnualMid + (decRangeSpreadResults * decAnnualMid)).ToString("C")

            ''Display results in dollar string in Hourly rate
            lblHourlyMin.Text = Convert.ToDecimal(decAnnualMid + (decRangeSpreadResults * decAnnualMid) / 52 / 40).ToString("C")
            lblHourlyMax.Text = Convert.ToDecimal(decAnnualMid + (decRangeSpreadResults * decAnnualMid) / 52 / 40).ToString("C")

        Else

            MsgBox("You have entered a non-numeric value. Please check value and enter again", vbCritical, "Input Error")


            With txtRangeSpread
                .Text = ""
                .Focus()

            End With

        End If


    End Sub
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-24 12:52:22

实际上你想要的是这个

代码语言:javascript
复制
  decimal.TryParse("$35,000",NumberStyles.Currency ,null , out test)

例如:

代码语言:javascript
复制
    using System;
    using System.Globalization;

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                decimal test;
                Console.WriteLine(decimal.TryParse("$35,000",NumberStyles.Currency ,null , out test));
                Console.WriteLine(test);
                Console.Read();
            }
        }
    }

more information about NumberStyles

票数 0
EN

Stack Overflow用户

发布于 2013-06-24 10:39:58

我将我的代码行改为:

代码语言:javascript
复制
Decimal.TryParse(lblAnnualMid.Text.Replace("$", ""), decAnnualMid)

这工作得很完美。

谢谢你们所有人。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17266849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档