Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

Format a numeric string

Whan I format numeric string  in VB6.0
Fromat literal may be # or 0
? format(11-2424557,"##-#######")
--2424546  ( Result)
Expecting Result
11-2424557
0
NasheerIT
Asked:
NasheerIT
1 Solution
 
venkateshwarrCommented:
It should be

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

u r getting --2424546  bcoz  

11 minus 2424557 is -2424546 .
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
bruintjeCommented:
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
 
Corey ScheichDeveloperCommented:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now