Results 1 to 17 of 17

Thread: [RESOLVED] Problem formatting an array

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    24

    Resolved [RESOLVED] Problem formatting an array

    I need to format every number in MyArray with a thousand separator. I want it to add ",".

    Since the numbers in the array will be <= maximum value of a Long, which is 2,147,483,647.

    What should the "???????" be replaced with? .
    Code:
    For i = LBound(MyArray) To UBound(MyArray)
    Myarray(i) = Format(Myarray(i), ???????)
    Next
    Last edited by MrDudemeister; Nov 19th, 2008 at 08:53 AM.

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Problem formatting an array

    For thousand's separetor

    Try This

    Code:
    For i = LBound(Myarray) To UBound(Myarray)
        Myarray(i) = Format(Myarray(i), "#,##0.00")
    Next
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    24

    Re: Problem formatting an array

    Hmm, that code just adds ",00" at the end of the number. I need it to add "," between every thousand.

    Like this: (1,000) (10,000) (100,000) (1,000,000).

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Problem formatting an array

    hmmm, let me check that for you....

    I just tried it, it works fine Yes it does add the decimal. If you don't want that then simply remove it. for example

    Msgbox Format(100000,"#,##")

    will give you 100,000
    Last edited by Siddharth Rout; Nov 19th, 2008 at 09:37 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    24

    Re: Problem formatting an array

    Well then the problem seems to be with the listbox. You see I format the number just before I add them to a listbox. Maybe the listbox replace "," with a blank space somehow?

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Problem formatting an array

    Okay Try this

    Code:
    Sub Testit()
    '
    'Your Code
    '
    For i = LBound(Myarray) To UBound(Myarray)
        Myarray(i) = Format(Myarray(i), "#,##")
        MsgBox Myarray(i) '<===== Try this
    Next
    End Sub
    Does it display the relevant numbers?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    24

    Re: Problem formatting an array

    Nope, it still displays them with a blank space. Ex: (700 000 000)

  8. #8
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Problem formatting an array

    ok just try this single line of code...

    Msgbox Format(100000,"#,##")

    What does it give you?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    24

    Re: Problem formatting an array

    Lol, still a blank space; this is getting weird .

  10. #10
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Problem formatting an array

    Builld a form with a list box and try this:
    Code:
    Private Sub Form_Load()
    or I = 1 To 1000
        List1.AddItem Format$(Int(Rnd * 2000000000), "#,###,###,###")
    Next
    End Sub
    You should get 1000 random numbers all formatted with commas in the right position.
    Doctor Ed

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    24

    Re: Problem formatting an array

    I don't. Instead of commas I get blank spaces. Anyone know why?

  12. #12
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Problem formatting an array

    Hi Code

    I could be wrong but this won't work in his case (see post 6-9)
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

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

    Re: Problem formatting an array

    MsgBox FormatNumber$(&H7FFFFFFF, , , , vbTrue)

    Note that results depend on locale settings: most VB functions are locale aware, thus you get results according to which country the computer is located at.


    If you wish to force to have commas every X character, you have to do it by yourself.

    Code:
    Public Function Comma(ByVal Expression As String, Optional ByVal Spacing As Byte = 3) As String
        Dim lngA As Long, lngLen As Long
        If (Len(Expression) > Spacing) And (Spacing > 0) Then
            lngLen = (Len(Expression) \ Spacing) + Len(Expression) + ((Len(Expression) Mod Spacing) = 0)
            Comma = String$(lngLen, ",")
            lngLen = lngLen - Spacing + 1
            For lngA = 1 To Len(Expression) \ Spacing
                Mid$(Comma, lngLen, Spacing) = Mid$(Expression, Len(Expression) - (lngA * Spacing) + 1, Spacing)
                lngLen = lngLen - Spacing - 1
            Next lngA
            lngLen = lngLen + Spacing - 1
            If lngLen > 0 Then
                Mid$(Comma, 1, lngLen) = Left$(Expression, lngLen)
            End If
        Else
            Comma = Expression
        End If
    End Function
    MsgBox Comma(&H7FFFFFFF)

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    24

    Re: Problem formatting an array

    Thanks a lot for the help. It works with the code provided by: Merri.

  15. #15
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: [RESOLVED] Problem formatting an array

    Merri you beat me to it

    or Go to "control panel", and set the country "local configuration" to English (US).
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  16. #16
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Problem formatting an array

    Quote Originally Posted by koolsid
    Hi Code, I could be wrong but this won't work in his case (see post 6-9)
    I saw those in somewhat disbelief. Merri has the solution--machine settings. I suspected that. So, after we build the spaces, we can force the commas back into the string. This is short and sweet:
    Code:
    Private Sub Form_Load()
    Dim MyNum As String
    For I = 1 To 1000
        MyNum = Format$(Int(Rnd * 2000000000), "# ### ### ###")
        MyNum = Replace(MyNum, " ", ",")
        If Left$(MyNum, 1) = "," Then MyNum = Mid$(MyNum, 2)
        List1.AddItem MyNum
    Next
    End Sub
    Doctor Ed

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

    Re: [RESOLVED] Problem formatting an array

    Code Doc: sorry, that still isn't guaranteed to work. There are maybe around 200 different locale settings out there that can each be dramatically different from each other. How can you guarantee that it'll work unless you have investigated and ensured it'll work with each locale?

    Of course most of the locale settings for numbers are quite similar, but there can still be some surprises.

    Thus the only way you can guarantee something to work is to avoid using a locale function.


    As for other sample, there are many ways to understand 11/11/2001 - it could be MM/DD/YYYY like in the US – or it could be DD/MM/YYYY like in some European countries. This is why one shouldn't use CDate() to convert strings to dates in some cases. Worst thing to do is to use hard coded string dates.

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