[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:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim d As Decimal
d = (TextBox2.Text.ToString / TextBox1.Text.ToString)
Me.TextBox3.Text = Decimal.Round(d, 4).ToString("f6")
End Sub
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.
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.
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?
Re: Taking Result And Converting to Percentage
vb Code:
percentage = (result * 100).tostring("n2")
Re: Taking Result And Converting to Percentage
Quote:
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.
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.
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.
Re: Taking Result And Converting to Percentage
Thanks .paul. that did it :-)