Results 1 to 10 of 10

Thread: Formatting Numbers to Display A Certain Way in Labels

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2008
    Location
    St. John's, Newfoundland, Canada
    Posts
    965

    Formatting Numbers to Display A Certain Way in Labels

    I would like to know how to code a number that ends with "000000" and display that particular number in a label as "# MIL".

    Like, say, if I had to enter in a text box the number 56000000, then instead of displaying the number as "56000000" in the label, it should display as "56 MIL" (MIL meaning million).

    Also, for numbers higher than 9999 but lower than 1000000, the number should display a comma to distinguish the thousands. How do I do this, along with the "MIL" scenario?

  2. #2
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    804

    Re: Formatting Numbers to Display A Certain Way in Labels

    Many uses for the Format$ function.
    Code:
     Dim Mega As Long
     Dim Num As Long
     Mega = 10 ^ 6
     Num = 1550 * Mega
     Debug.Print Format$(Num / Mega, "# Mil")
     Debug.Print Format$(Num, "###,##0")
     Debug.Print Format$(Num / Mega, "###,##0 MIL")

  3. #3
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Formatting Numbers to Display A Certain Way in Labels

    This may help:
    Code:
        Text1.Text = "56000000"
        'Text1.Text = "56000012"
        'Text1.Text = "12345"
        
        Dim n As Long
        n = Val(Text1.Text)
        Label1.Caption = Format(n, IIf(n <> 0 And n Mod 1000000 = 0, "#,,"" MIL""", "#,##0"))
    Code:
    56 MIL
    56,000,012
    12,345
    Last edited by anhn; Jun 16th, 2010 at 09:02 AM. Reason: Remove Abs()
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Formatting Numbers to Display A Certain Way in Labels

    If IsNumeric(Text1.Text) Then Label1.Caption = Int(Text1.Text / 10000@) / 100 & " MIL"


    "56000000" -> 56 MIL
    "56000012" -> 56 MIL
    "56120000" -> 56,12 MIL
    "12345" -> 0,01 MIL


    This is what I understood from the last paragraph of the OP, don't know whether it is wanted this way or not.
    Last edited by Merri; Jun 16th, 2010 at 09:19 AM. Reason: Changed datatype

  5. #5
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Formatting Numbers to Display A Certain Way in Labels

    Quote Originally Posted by JonSea31 View Post
    I would like to know how to code a number that ends with "000000" and display that particular number in a label as "# MIL".

    Like, say, if I had to enter in a text box the number 56000000, then instead of displaying the number as "56000000" in the label, it should display as "56 MIL" (MIL meaning million).

    Also, for numbers higher than 9999 but lower than 1000000, the number should display a comma to distinguish the thousands. How do I do this, along with the "MIL" scenario?
    I understand differently.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2008
    Location
    St. John's, Newfoundland, Canada
    Posts
    965

    Re: Formatting Numbers to Display A Certain Way in Labels

    Quote Originally Posted by anhn View Post
    This may help:
    Code:
        Text1.Text = "56000000"
        'Text1.Text = "56000012"
        'Text1.Text = "12345"
        
        Dim n As Long
        n = Val(Text1.Text)
        Label1.Caption = Format(n, IIf(n <> 0 And n Mod 1000000 = 0, "#,,"" MIL""", "#,##0"))
    Code:
    56 MIL
    56,000,012
    12,345
    And what about percentages? I would like to learn to format percentages (with a percent sign).
    Last edited by JonSea31; Jun 18th, 2010 at 08:04 PM.

  7. #7
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Formatting Numbers to Display A Certain Way in Labels

    MsgBox (PartValue / TotalValue * 100) & " &#37;"

    Note that this will generate an error of TotalValue would be 0.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2008
    Location
    St. John's, Newfoundland, Canada
    Posts
    965

    Re: Formatting Numbers to Display A Certain Way in Labels

    I was able to figure out the Billion scenario, but it can only go so far with the Long command. What would I have to use instead of "Long" to accommodate larger numbers?

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2008
    Location
    St. John's, Newfoundland, Canada
    Posts
    965

    Re: Formatting Numbers to Display A Certain Way in Labels

    Quote Originally Posted by Merri View Post
    MsgBox (PartValue / TotalValue * 100) & " %"

    Note that this will generate an error of TotalValue would be 0.
    Actually, I was thinking about reading a number from a text file and displaying it in percentage form.

  10. #10
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Formatting Numbers to Display A Certain Way in Labels

    (EDIT: You may figure out anything yourself)
    by searching VB Help on Format() function on how to define FormatString.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

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