msvisual.com Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Getting Number in 12,34,56,789 this format

 
Post new topic   Reply to topic    msvisual.com Forum Index -> VB Enterprise
Author Message
Chirag



Joined: 04 Oct 2007
Posts: 13

PostPosted: Wed Sep 17, 2003 3:10 am    Post subject: Getting Number in 12,34,56,789 this format Reply with quote

I want to get Numbers

in

12,34,56,789

As it is needed that the commas should be in such a position and not in a
millions Style

I tried Format() But do not know Which User Defined Format to give

Waiting for Help

Thanks in Advance

Archived from group: microsoft>public>vb>enterprise
Back to top
View user's profile Send private message
Al Reid



Joined: 04 Oct 2007
Posts: 5

PostPosted: Tue Sep 16, 2003 5:59 pm    Post subject: Re: Getting Number in 12,34,56,789 this format Reply with quote

Here is an example that works for the specific case you provided.

Format$("12345678","##,##,##,###")

I am sure you can adapt it for the general case Wink

Al Reid

"Chirag" wrote in message@TK2MSFTNGP10.phx.gbl...
> I want to get Numbers
>
> in
>
> 12,34,56,789
>
> As it is needed that the commas should be in such a position and not in a
> millions Style
>
> I tried Format() But do not know Which User Defined Format to give
>
> Waiting for Help
>
> Thanks in Advance
>
>
Back to top
View user's profile Send private message
Ken Halter



Joined: 04 Oct 2007
Posts: 4150

PostPosted: Tue Sep 16, 2003 2:58 pm    Post subject: Re: Getting Number in 12,34,56,789 this format Reply with quote

This seems to do the trick....
'================
Option Explicit

Private Sub Command1_Click()
Debug.Print CustomFormat(123456789)
Debug.Print CustomFormat(23456789)
Debug.Print CustomFormat(3456789)
Debug.Print CustomFormat(456789)
Debug.Print CustomFormat(56789)
Debug.Print CustomFormat(6789)
Debug.Print CustomFormat(789)
Debug.Print CustomFormat(89)
Debug.Print CustomFormat(9)
End Sub

Private Function CustomFormat(NumIn As Long) As String
Dim sStr As String
Dim sFuncReturn As String
Dim iSection As Integer
Dim iNewLen As Integer
Dim sSeparator As String

sStr = "" & NumIn
iSection = 3
Do
sFuncReturn = Right$(sStr, iSection) & sSeparator & sFuncReturn
iNewLen = Len(sStr) - iSection
If iNewLen > 0 Then
sStr = Left$(sStr, Len(sStr) - iSection)
iSection = 2
sSeparator = ","
Else
Exit Do
End If
Loop While Len(sStr) > 0

CustomFormat = sFuncReturn
End Function
'================

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep it in the groups..


"Chirag" wrote in message@TK2MSFTNGP10.phx.gbl...
> I want to get Numbers
>
> in
>
> 12,34,56,789
>
> As it is needed that the commas should be in such a position and not in a
> millions Style
>
> I tried Format() But do not know Which User Defined Format to give
>
> Waiting for Help
>
> Thanks in Advance
>
>
Back to top
View user's profile Send private message
Ken Halter



Joined: 04 Oct 2007
Posts: 4150

PostPosted: Tue Sep 16, 2003 2:59 pm    Post subject: Re: Getting Number in 12,34,56,789 this format Reply with quote

Actually, that displays the normal formatting. 12,345,678

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep it in the groups..


"Al Reid" wrote in message%23tHfDHA.2984@TK2MSFTNGP11.phx.gbl...
> Here is an example that works for the specific case you provided.
>
> Format$("12345678","##,##,##,###")
>
> I am sure you can adapt it for the general case Wink
>
> Al Reid
Back to top
View user's profile Send private message
Al Reid



Joined: 04 Oct 2007
Posts: 5

PostPosted: Tue Sep 16, 2003 6:20 pm    Post subject: Re: Getting Number in 12,34,56,789 this format Reply with quote

oops!! I tried it in the immediate window with my eyes closed. Looking
back at it, it's plain as day.

Al

"Ken Halter" wrote in message@TK2MSFTNGP12.phx.gbl...
> Actually, that displays the normal formatting. 12,345,678
>
> --
> Ken Halter - MS-MVP-VB - http://www.vbsight.com
> Please keep it in the groups..
>
>
> "Al Reid" wrote in message
> %23tHfDHA.2984@TK2MSFTNGP11.phx.gbl...
> > Here is an example that works for the specific case you provided.
> >
> > Format$("12345678","##,##,##,###")
> >
> > I am sure you can adapt it for the general case Wink
> >
> > Al Reid
>
>
Back to top
View user's profile Send private message
Al Reid



Joined: 04 Oct 2007
Posts: 5

PostPosted: Tue Sep 16, 2003 7:05 pm    Post subject: Re: Getting Number in 12,34,56,789 this format Reply with quote

Ken,

Yours is better but, I think this one at least works:

Public Function CustomFormat(ByVal inNumber As Long) As String

Dim intPos As Integer
Dim strNumber As String
Dim intNumLen As Integer

strNumber = CStr(inNumber)
intNumLen = Len(strNumber)
For intPos = intNumLen To 1 Step -1
If (intNumLen - intPos > 2) And ((intPos - 1 - (intNumLen Mod 2)) Mod
2 = 0) Then
strNumber = Replace(strNumber, Mid$(CStr(inNumber), intPos, 1),
Mid$(CStr(inNumber), intPos, 1) & ",", , 1)
End If
Next

CustomFormat = strNumber

End Function

Al

"Ken Halter" wrote in message@TK2MSFTNGP12.phx.gbl...
> Actually, that displays the normal formatting. 12,345,678
>
> --
> Ken Halter - MS-MVP-VB - http://www.vbsight.com
> Please keep it in the groups..
>
>
> "Al Reid" wrote in message
> %23tHfDHA.2984@TK2MSFTNGP11.phx.gbl...
> > Here is an example that works for the specific case you provided.
> >
> > Format$("12345678","##,##,##,###")
> >
> > I am sure you can adapt it for the general case Wink
> >
> > Al Reid
>
>
Back to top
View user's profile Send private message
Albert Reid



Joined: 04 Oct 2007
Posts: 12

PostPosted: Tue Sep 16, 2003 7:35 pm    Post subject: Re: Getting Number in 12,34,56,789 this format Reply with quote

at least sometimes...

Al Reid

> Ken,
>
> Yours is better but, I think this one at least works:
>
> Public Function CustomFormat(ByVal inNumber As Long) As String
>
> Dim intPos As Integer
> Dim strNumber As String
> Dim intNumLen As Integer
>
> strNumber = CStr(inNumber)
> intNumLen = Len(strNumber)
> For intPos = intNumLen To 1 Step -1
> If (intNumLen - intPos > 2) And ((intPos - 1 - (intNumLen Mod 2))
Mod
> 2 = 0) Then
> strNumber = Replace(strNumber, Mid$(CStr(inNumber), intPos, 1),
> Mid$(CStr(inNumber), intPos, 1) & ",", , 1)
> End If
> Next
>
> CustomFormat = strNumber
>
> End Function
>
> Al
>
> "Ken Halter" wrote in message
> @TK2MSFTNGP12.phx.gbl...
> > Actually, that displays the normal formatting. 12,345,678
> >
> > --
> > Ken Halter - MS-MVP-VB - http://www.vbsight.com
> > Please keep it in the groups..
> >
> >
> > "Al Reid" wrote in message
> > %23tHfDHA.2984@TK2MSFTNGP11.phx.gbl...
> > > Here is an example that works for the specific case you provided.
> > >
> > > Format$("12345678","##,##,##,###")
> > >
> > > I am sure you can adapt it for the general case Wink
> > >
> > > Al Reid
> >
> >
>
>

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    msvisual.com Forum Index -> VB Enterprise All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group