Results 1 to 9 of 9

Thread: [RESOLVED] Taking Result And Converting to Percentage

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    159

    Resolved [RESOLVED] Taking Result And Converting to Percentage

    I'm trying to take a result for a simple website conversion calculator and turn that into a percentage. So for example, if the result is 0.314500 I want to turn that into 31.4%.

    Here is the code I have so far for this:

    Visual Basic 2008 Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim d As Decimal
    3.         d = (TextBox2.Text.ToString / TextBox1.Text.ToString)
    4.         Me.TextBox3.Text = Decimal.Round(d, 4).ToString("f6")
    5.     End Sub

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Taking Result And Converting to Percentage

    Why are you calling ToString on the text property of a textbox ? It is already a string.

    You should be converting to a numeric type in order for your calculation to make sense.

    I'd recommend turning Option Strict on and fixing the errors that highlights.

  3. #3
    Lively Member
    Join Date
    Nov 2008
    Posts
    89

    Re: Taking Result And Converting to Percentage

    Why don't you multiply your decimal by 100 and then cut off however many places after the decimal point you want.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    159

    Re: Taking Result And Converting to Percentage

    I tried turning it on and got no difference...I will remove the .tostring reference but do you have example code to take the result and do the conversion?

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Taking Result And Converting to Percentage

    vb Code:
    1. percentage = (result * 100).tostring("n2")

  6. #6
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Taking Result And Converting to Percentage

    do you have example code to take the result and do the conversion?
    How about

    Code:
            Dim d As Decimal
            d = (Decimal.Parse(TextBox2.Text) / Decimal.Parse(TextBox1.Text))
            MessageBox.Show(Decimal.Round(d, 4).ToString("f1"))
    For simplicity I'm assuming that you've already validated that textbox2.text and textbox1.text both contain text that can be resolved to decimal values.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    159

    Re: Taking Result And Converting to Percentage

    Ok t3kizzleee I did that and it did give me the result but I'd like to have it cut off all but lets say 31.4% as an example result.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Taking Result And Converting to Percentage

    keystone paul was talking about performing mathematical operations on strings, which you should never do. option strict on, wouldn't allow that.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    159

    Re: Taking Result And Converting to Percentage

    Thanks .paul. that did it :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width