Solved

Format a numeric string

Posted on 2004-09-14
6
230 Views
Last Modified: 2010-04-17
Whan I format numeric string  in VB6.0
Fromat literal may be # or 0
? format(11-2424557,"##-#######")
--2424546  ( Result)
Expecting Result
11-2424557
0
Comment
Question by:NasheerIT
6 Comments
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 12061807
It should be

format(112424557,"##-#######")

u r getting --2424546  bcoz  

11 minus 2424557 is -2424546 .
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 25 total points
ID: 12061811
This will give you what you are expecting:
    Debug.Print Format("112424557", "##-#######")  ' Notice the quotation marks around the number!

The problem with:
   ? format(11-2424557,"##-#######")
is that the 11-2424557 part is evaluated first as numerics, giving you -2424546.  This only fills the right half of your format string giving you 00--2424546 (two negatives because one is from the format and one is from the negative value) but since you used ## instead of 00 it doesn't display the leading zeroes leaving you with just --2424546.

Regards,

Idle_Mind
0
 
LVL 44

Expert Comment

by:bruintje
ID: 12061815
that's only working if you try numeric values not string values

? format(112424557,"##-#######")

The Format function converts a value to a text string and gives you control over the string's appearance

title : Using the Visual Basic format() function
source : http://www.apostate.com/programming/vb-format.html
0
 
LVL 13

Expert Comment

by:Corey2
ID: 12066225
I don't know if there is a shorter way but this works

Sub test()
    MsgBox ReturnFormated(112424557)
    MsgBox ReturnFormated("11-2424557")
End Sub

Function ReturnFormated(Value As Variant) As String

    Dim MyString As String
    Dim TempString As Variant
    Dim MyStringArray() As String
   
    If TypeName(Value) = "String" Then
        MyStringArray = Split(Value, "-", -1)
        MyString = ""
        For Each TempString In MyStringArray
            MyString = MyString & TempString
        Next
        Value = MyString
    End If
   
    ReturnFormated = Format(Value, "##-#######")

End Function
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now