Results 1 to 9 of 9

Thread: [RESOLVED] [2005] Formatting numbers

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    30

    Resolved [RESOLVED] [2005] Formatting numbers

    I would like to know if there is a formatting type for formatting numbers that will display the number in a label with commas to the left of the decimal, and show the digits to the right of the decimal without zeroes at the end. I didn't find what i am looking for on msdn, and am wondering whether or not i am out of luck, or if there actually is a way of doing it.

    Example: 1234.56789
    ideally, i would like the number to be displayed in the label as: 1,234.56789
    the only thing i found was format number, but it shows 0's at the end of the number up until whatever number of digits you tell it to round to.
    so if i were to tell it to round to 7 digits, it would give me 1,234.5678900, which i do not want.

    Thanks

  2. #2
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2005] Formatting numbers

    i believe you can use .ToString() method.. hold on a second

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    30

    Re: [2005] Formatting numbers

    so are you saying to do:
    VB Code:
    1. label1.text = var.tostring("(format type)")
    My question is what to put in the brackets in order for the label to display the number how i would like it to. I've tried just about all of the formatting types, but none of them gave me the results i am looking for. I guess i may have missed one though...

  4. #4
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2005] Formatting numbers

    Code:
        Private Sub btnFormat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFormat.Click
            ' lblNumber.Text = 1234.56789000
            Dim aNumber As Double = Double.Parse(lblNumber.Text)
            ' If you parse it to a double ^, then
            ' it gets rid of the zeros at the end
    
            ' Now to add digit grouping
            Dim aNewNumber As Double = aNumber.ToString()
            lblOutput.Text = aNewNumber.ToString("n5")
        End Sub
    As you can see, I used n5. What is n5 you ask? Well, n stands for Number and 5 is the number of digits after 1234. What I didn't ask is, do the number of digits change? In which case, this may not work as well. If you were wondering, I found this information at:

    http://msdn2.microsoft.com/en-us/library/241ad66z.aspx

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    30

    Re: [2005] Formatting numbers

    yes, i guess i forgot to mention that the number of digits do change...sorry for any inconvenience. Now is it possible for this to work when the number of digits do change?

  6. #6
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2005] Formatting numbers

    ahh quite ok i think i found something else on msdn that could work

  7. #7
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2005] Formatting numbers

    Code:
        Private Sub btnFormat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFormat.Click
            Dim nfi As System.Globalization.NumberFormatInfo = New System.Globalization.CultureInfo("en-US", False).NumberFormat
    
            lblNumber.Text = Double.Parse(lblNumber.Text)
            Dim decimalLength As Integer = lblNumber.Text.Remove(0, lblNumber.Text.IndexOf(".") + 1).Length
    
            nfi.NumberDecimalDigits = decimalLength
            Dim aNumber As Double = Double.Parse(lblNumber.Text)
            lblOutput.Text = aNumber.ToString("N", nfi)
        End Sub
    Sorry for the wait, I was thinking of the best way to do this and I got tired of thinking and just picked one. Also, I had to eat dinner.

    If there is anything u don't get... *siggy*

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    30

    Re: [2005] Formatting numbers

    Alright, thanks for the help. I tested the code, and it works quite well. You may or may not hear from me on msn...anywaysm your help and time are much appreciated.

  9. #9
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [RESOLVED] [2005] Formatting numbers

    no prob

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