|
-
Feb 18th, 2010, 09:30 AM
#1
Thread Starter
Addicted Member
Decimals revert to 0
I have a textbox that requires the user to input the value of the Value-Added Tax of that certain area. I tried testing 12.5%. The string is converted to decimal then divided by 100 so 12.5/100 = .125. But when the program writes the value to a file, 0 is writen. not .125
-
Feb 18th, 2010, 09:43 AM
#2
Re: Decimals revert to 0
how are you writing it to the file? Are you storing the value in a variable? Make sure that it is a decimal type, and not an integer...
-tg
-
Feb 18th, 2010, 09:52 AM
#3
Thread Starter
Addicted Member
Re: Decimals revert to 0
Yes. It's in decimal
The code's like this
Code:
Dim q As Decimal
q = txtTax.Text
I tried using
q = cdec(txtTax.Text)
but it doesn't work either.
-
Feb 18th, 2010, 10:23 AM
#4
Re: Decimals revert to 0
Need a bit more than that. How'd txtTax get involved? Where are you doing your division at? Where are you writing to the file? I don't want ALL of the code in your project, but at least the bits that are doing your calculations and writing to file.
-tg
-
Feb 18th, 2010, 11:35 AM
#5
Re: Decimals revert to 0
Yeah, it's hard to look at this and not assume that you are converting the decimal to an integer at some point. Since you were able to do this:
Dim q As Decimal
q = txtTax.Text
you obviously have Option Strict OFF. Try turning it on and fixing the errors that show up. One of them will probably be an implicit conversion of decimal to Integer. Option strict catches bugs like that, as well as making code run faster and promoting good habits.
My usual boring signature: Nothing
 
-
Feb 18th, 2010, 04:53 PM
#6
Re: Decimals revert to 0
Exactly what Shaggy said. Your problem is you have Option Strict = Off. Turn it on and specify your conversions.
-
Feb 19th, 2010, 04:59 AM
#7
Thread Starter
Addicted Member
Re: Decimals revert to 0
The user is aksed to input a number (In percentage) into a textbox(txtTax).
When Save button is clicked, the program will store the value to "q".
After storing, it will divide "q" by 100 so that it will be in decimal form.
No errors come up. The program runs but 0 is displayed when the value of tax in decimal form is displayed
-
Feb 19th, 2010, 05:06 AM
#8
Frenzied Member
Re: Decimals revert to 0
You don't mention that you turned Option Strict = On, which will provide you visual error messages that you can most probably fix instantly
-
Feb 19th, 2010, 05:13 AM
#9
Thread Starter
Addicted Member
Re: Decimals revert to 0
I've turned Option Strict to on but how will I see the error?
-
Feb 19th, 2010, 05:29 AM
#10
Frenzied Member
Re: Decimals revert to 0
I took a screen shot of what you should see but i am unable to post here for some reason
-
Feb 19th, 2010, 05:31 AM
#11
Thread Starter
Addicted Member
Re: Decimals revert to 0
Maybe you've reached the maximum allowed space?
-
Feb 19th, 2010, 07:57 AM
#12
Re: Decimals revert to 0
q is a number. What you are writing isn't that number, but the string representation of the number. The documentation has many examples of how to change things like numbers and dates to their string representation.
http://msdn.microsoft.com/en-us/library/26etazsy.aspx
-
Feb 19th, 2010, 08:00 AM
#13
Thread Starter
Addicted Member
Re: Decimals revert to 0
but if the value i enter is above 100, the value 1 is in the variable. it is only when the value is less than 100 when the value is 0. I know this happens when a variable is declared as an integer but the declaration is decimal
-
Feb 19th, 2010, 08:13 AM
#14
Re: Decimals revert to 0
 Originally Posted by azsx123
but if the value i enter is above 100, the value 1 is in the variable. it is only when the value is less than 100 when the value is 0. I know this happens when a variable is declared as an integer but the declaration is decimal
Did you go to the link I posted and read the parts pertaining to numbers?
-
Feb 19th, 2010, 08:16 AM
#15
Re: Decimals revert to 0
Is this
Code:
Dim q As Decimal = 12.5D
q = q / 100
TextBox1.Text = q
what you are doing?
-
Feb 19th, 2010, 08:17 AM
#16
Re: Decimals revert to 0
CODE! PLEASE! Can we see some CODE? Clearly there is something that isn't right.
-tg
-
Feb 19th, 2010, 08:22 AM
#17
Thread Starter
Addicted Member
Re: Decimals revert to 0
This is part of the code where the value is transferred, converted, and written to a file.
Code:
q = CDec(txtTax.Text)
w = q / 100
Dim fs As New FileStream("Setting.conf", FileMode.OpenOrCreate, FileAccess.ReadWrite)
Dim s As New StreamWriter(fs)
info.StoreName = txtStoreName.Text
info.aName = txtAName.Text
info.aID = txtAID.Text
info.tax = w
s.BaseStream.Seek(0, SeekOrigin.Begin)
s.WriteLine(info.StoreName)
s.WriteLine(info.aName)
s.WriteLine(info.aID)
s.WriteLine(info.tax)
s.Close()
btnOk.Enabled = btnSave.Enabled
This is the code for computation
Code:
Dim i As Integer = 0
If i <= 29 Then
total += Quantity(i) * price(i)
i += 1
End If
vat = total * info.tax
subtotal = total - vat
i = 0
Me.Hide()
frmCheckout.Show()
vat is always 0
Edit:@ dbasnett
yes i'm currently reading
@dbasnett
no
-
Feb 19th, 2010, 08:45 AM
#18
-
Feb 19th, 2010, 08:46 AM
#19
Thread Starter
Addicted Member
Re: Decimals revert to 0
Sorry missed that part.
dim q, w As Decimal
-
Feb 19th, 2010, 08:48 AM
#20
-
Feb 19th, 2010, 09:03 AM
#21
Re: Decimals revert to 0
What I have so far.
Code:
Dim q, w As Decimal
'textbox1 = 12.5
If Decimal.TryParse(TextBox1.Text.Trim, q) Then
'good number, q = the number at this point
w = q / 100 'convert to percent
End If
-
Feb 19th, 2010, 09:40 AM
#22
Re: Decimals revert to 0
What is "info"? And how is info.tax defined? And vat .... and I'm not sure your calculations are correct... you're using total times tax to get to the VAT... but then take the VAT out of the total to get the subtotal.... sub total should be the sum total of all items times the price. The VAT should be calculated off that amount... the subtotal plus the VAT should then be the total.... or does VAT not work that way?
-tg
-
Feb 19th, 2010, 09:44 AM
#23
Re: Decimals revert to 0
And while we are at it, what is the definition for vat?
My usual boring signature: Nothing
 
-
Feb 20th, 2010, 05:03 AM
#24
Thread Starter
Addicted Member
Re: Decimals revert to 0
Works fine now. Calculation part is correct. I send the code to a friend. It works now. Dunno what he did.
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
|