Results 1 to 20 of 20

Thread: Decimal Formatting Help

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Decimal Formatting Help

    Hello fellow forumers! I have been having this issue, I have made this program that basically counts how many say tenths are in a given range like say 10 to 0. Well, in the Display when the data is being shown, I get an output of: 10, 9.9,9.8,9.7,9.6,9.5,9.4,9.3,9.2,9.1,9, etc...
    Well, I am wanting to make this program look professional so say where the 10 is, I would like it to say 10.0 or if they are say doing thousandths, it be like 10.000, Know what I mean? I have been to stackexchange, MSDN, but none pose a good solution to this matter. Any help works! Thanks

    Code Area:
    Code:
    'Where I want to format
            While (startNum >= EndNum + 0.1)
                counter += 1
                startNum -= 0.1
                Data += startNum & vbNewLine
                valprint += startNum & Space(4)
                pgbProgress.Increment(1)
            End While
            starting = txtStart.Text
            rtbDisplay.Text = starting & vbNewLine & Data & vbNewLine
            rtbVerticalText.Text = starting & Space(4) & valprint
            lblCount.Text = counter
            btnClear.Enabled = True
            btnCount.Enabled = False
            txtStart.Enabled = False
            txtEnd.Enabled = False
        End Sub
    Last edited by Dragnorian; May 11th, 2017 at 12:07 AM. Reason: Added Coding

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

    Re: Decimal Formatting Help

    Well, w/o seeing the code you have, it's hard to say what you should be doing differently.

    -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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Decimal Formatting Help

    When you call ToString on a numeric value, you can provide a format specifier to control what the output looks like. If you want one decimal place in the output then you can use "n1" or "f1" as the specifier. Change the number in the specifier to change the number of decimal places. You should read about numeric format strings in the MSDN documentation to learn more about what options there are.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: Decimal Formatting Help

    Added code, sorry about that

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: Decimal Formatting Help

    I read up on that before I came onto this post I made but where I thought I would put it which is where I declare what rtbDisplay.Text equals, just crashes the program. That is the main issue of finding where to put it lol. Putting it in the while statement also crashes program too.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Decimal Formatting Help

    You can only use a numeric format string when converting an actual number to a String. If you already have a String then you can't format it as a number. The place that you convert a number to a String is here:
    Code:
    Data += startNum & vbNewLine
    so that's where you should do the formatting:
    Code:
    Data &= startNum.ToString("n2") & vbNewLine
    Notice that I also changed '+=' to '&=". While you can use '+' to concatenate Strings in VB.NET, you should never do so. You should ALWAYS use '&'. That's because '+' is actually the addition operator and, while addition of two Strings is implemented as concatenation, addition of one String and something else is generally not, so '+' may not behave as expected in some circumstances. '&' is the concatenation operator and will ALWAYS behave as expected.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: Decimal Formatting Help

    Here are my declarations:
    Code:
        Dim startNum, starting, custom, progress As Decimal
        Dim total As Long
        Dim EndNum, increment As Decimal
        Dim Data, numdata As String
        Dim counter As Integer = 0
        Dim time As Integer = 1
        Dim TimeCount As Integer = 0
        Dim valprint As String
    I should make Data and Startnum and Endnum into string variables instead of Decimal variables? Therefore getting the results I want?

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: Decimal Formatting Help

    I tried that(hopefully the correct way) and it didn't work. I tried changing it to different ways but those didn't work either. I changed the startnum declaration to a string instead of a decimal like I thought would be the way to do it.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Decimal Formatting Help

    Post the code that you think is right and tell us what actually happens when you run it. "It doesn't work" is rarely very helpful. We need to know what doesn't work and how it doesn't work in order to know what needs to be done to fix it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: Decimal Formatting Help

    Code:
        Dim startNum, starting, custom, progress As String 'Changed to String
        Dim total As Long
        Dim EndNum, increment As Decimal
        Dim Data, numdata As String
        Dim counter As Integer = 0
        Dim time As Integer = 1
        Dim TimeCount As Integer = 0
        Dim valprint As String
    I changed the variable from a decimal to a string.

    Code:
                Data &= (startNum.ToString("n2") & vbNewLine) 'Can't Go Here
    I then convert the string to a string? That doesn't look right but I am still learning.

    After I run the program, the program crashes with an exception unhandled error.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Decimal Formatting Help

    What did I say in post #6?
    You can only use a numeric format string when converting an actual number to a String. If you already have a String then you can't format it as a number.
    Now let's look at your code:
    Quote Originally Posted by Dragnorian View Post
    Code:
        Dim startNum, starting, custom, progress As String 'Changed to String
        Dim total As Long
        Dim EndNum, increment As Decimal
        Dim Data, numdata As String
        Dim counter As Integer = 0
        Dim time As Integer = 1
        Dim TimeCount As Integer = 0
        Dim valprint As String
    I changed the variable from a decimal to a string.

    Code:
                Data &= (startNum.ToString("n2") & vbNewLine) 'Can't Go Here
    I then convert the string to a string? That doesn't look right but I am still learning.

    After I run the program, the program crashes with an exception unhandled error.
    Do you not see the issue there? People are commonly quite lax with data types when they start out and this is a perfect example of why they need to tighten up ASAP. If, as the name suggests, 'startNum' is supposed to represent a number then it should be declared as the appropriate numeric type. String is ONLY for text. If you want to treat something as a number then it needs to be a number, NOT text. You only convert numbers, dates, etc to text for the purposes of display or serialisation, e.g. saving to a text file. In your code, numbers should ALWAYS be stored in numeric variables.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: Decimal Formatting Help

    Yes I know that I Need to tighten up on it. I figured that was a bit strange, I would be lying if I said I never misread anything. I got it all now, thank you for teaching me this and sorry for the accident I made. This will surely help my program. I had been trying different formats(attempted) before coming here. Thank you!

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Decimal Formatting Help

    Quote Originally Posted by Dragnorian View Post
    Yes I know that I Need to tighten up on it.
    Pretty much everyone does to begin with. I think that it's often because data goes into and out of a console application as text and VB in particular can be quite forgiving in many cases, performing implicit conversions much of the time. Proper use of data types is critical to create software of any significance though.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: Decimal Formatting Help

    Yeah, I will be working on that. Thank you again for the help!

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: Decimal Formatting Help

    .,/'

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: Decimal Formatting Help

    That whole formatting issue was because the user can predetermine a set increment value that I have defined in the code but what about a custom increment value that the user defines, not me? I thought doing something like .ToString("n#") would work but no. I need something to figure out how it can calculate if the custom value is either an integer or a decimal number then figure out if decimal, how many places it should go to then do the same basic format as the .ToString("n1") does. Is this possible in Visual Basic?.

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Decimal Formatting Help

    E.g.
    vbn.et Code:
    1. Private Function GetFormatSpecifier(increment As Double) As String
    2.     Dim formatSpecifier As String
    3.     Dim text = increment.ToString()
    4.  
    5.     If text.Contains(".") Then
    6.         Dim fraction = text.Substring(text.IndexOf(".") + 1)
    7.  
    8.         formatSpecifier = "n" & fraction.Length
    9.     Else
    10.         formatSpecifier = "n0"
    11.     End If
    12.  
    13.     Return formatSpecifier
    14. End Function
    That's untested but I believe it should do the job. You can test and adjust if required.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: Decimal Formatting Help

    Okay, I will test it

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: Decimal Formatting Help

    I've never worked with functions before, so where would I put it? Inside the button?

  20. #20
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: Decimal Formatting Help

    If your using a form then just add it to your form. If your going to be calling it from multiple forms then declare the function "Public" and add it to a Module.

Tags for this Thread

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