Results 1 to 24 of 24

Thread: Decimals revert to 0

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2010
    Posts
    133

    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

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2010
    Posts
    133

    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.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  6. #6
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Decimals revert to 0

    Exactly what Shaggy said. Your problem is you have Option Strict = Off. Turn it on and specify your conversions.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2010
    Posts
    133

    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

  8. #8
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    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

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2010
    Posts
    133

    Re: Decimals revert to 0

    I've turned Option Strict to on but how will I see the error?

  10. #10
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    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

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jan 2010
    Posts
    133

    Re: Decimals revert to 0

    Maybe you've reached the maximum allowed space?

  12. #12
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jan 2010
    Posts
    133

    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

  14. #14
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Decimals revert to 0

    Quote Originally Posted by azsx123 View Post
    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?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  15. #15
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Decimals revert to 0

    Is this

    Code:
            Dim q As Decimal = 12.5D
            q = q / 100
            TextBox1.Text = q
    what you are doing?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  16. #16
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Decimals revert to 0

    CODE! PLEASE! Can we see some CODE? Clearly there is something that isn't right.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Jan 2010
    Posts
    133

    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

  18. #18
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Decimals revert to 0

    How is w defined?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Jan 2010
    Posts
    133

    Re: Decimals revert to 0

    Sorry missed that part.
    dim q, w As Decimal

  20. #20
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Decimals revert to 0

    Is Info.Tax a string?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  21. #21
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  22. #22
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  23. #23
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Decimals revert to 0

    And while we are at it, what is the definition for vat?
    My usual boring signature: Nothing

  24. #24

    Thread Starter
    Addicted Member
    Join Date
    Jan 2010
    Posts
    133

    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
  •  



Click Here to Expand Forum to Full Width