Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Format a numeric string

Posted on 2004-09-14
6
Medium Priority
?
268 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 100 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
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…
Starting up a Project

660 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