|
-
Mar 13th, 2010, 03:26 PM
#1
Thread Starter
Addicted Member
[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
-
Mar 13th, 2010, 03:29 PM
#2
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.
-
Mar 13th, 2010, 03:32 PM
#3
Lively Member
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.
-
Mar 13th, 2010, 03:34 PM
#4
Thread Starter
Addicted Member
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?
-
Mar 13th, 2010, 03:34 PM
#5
Re: Taking Result And Converting to Percentage
vb Code:
percentage = (result * 100).tostring("n2")
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 13th, 2010, 03:37 PM
#6
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.
-
Mar 13th, 2010, 03:37 PM
#7
Thread Starter
Addicted Member
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.
-
Mar 13th, 2010, 03:38 PM
#8
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 13th, 2010, 03:43 PM
#9
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|