Solved

Format a numeric string

Posted on 2004-09-14
6
219 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

763 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

9 Experts available now in Live!

Get 1:1 Help Now