|
-
Nov 19th, 2008, 08:47 AM
#1
Thread Starter
Junior Member
[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.
-
Nov 19th, 2008, 09:09 AM
#2
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
-
Nov 19th, 2008, 09:21 AM
#3
Thread Starter
Junior Member
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).
-
Nov 19th, 2008, 09:33 AM
#4
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
-
Nov 19th, 2008, 09:40 AM
#5
Thread Starter
Junior Member
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?
-
Nov 19th, 2008, 09:43 AM
#6
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
-
Nov 19th, 2008, 09:46 AM
#7
Thread Starter
Junior Member
Re: Problem formatting an array
Nope, it still displays them with a blank space. Ex: (700 000 000)
-
Nov 19th, 2008, 09:52 AM
#8
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
-
Nov 19th, 2008, 09:53 AM
#9
Thread Starter
Junior Member
Re: Problem formatting an array
Lol, still a blank space; this is getting weird .
-
Nov 19th, 2008, 10:21 AM
#10
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.
-
Nov 19th, 2008, 10:23 AM
#11
Thread Starter
Junior Member
Re: Problem formatting an array
I don't. Instead of commas I get blank spaces. Anyone know why?
-
Nov 19th, 2008, 10:23 AM
#12
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
-
Nov 19th, 2008, 10:26 AM
#13
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)
-
Nov 19th, 2008, 10:31 AM
#14
Thread Starter
Junior Member
Re: Problem formatting an array
Thanks a lot for the help. It works with the code provided by: Merri.
-
Nov 19th, 2008, 10:32 AM
#15
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
-
Nov 19th, 2008, 11:02 AM
#16
Re: Problem formatting an array
 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
-
Nov 19th, 2008, 11:11 AM
#17
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|