Solved

Format a numeric string

Posted on 2004-09-14
6
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 86

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn different types of Android Layout and some basics of an Android App.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
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…

752 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