Results 1 to 7 of 7
  1. #1
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Mundelein, Illinois, USA
    Posts
    198
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Format String Confusion (VB.NET 2003)

    I have a piece of code where I print using
    Thanks,

    C DeWitt

  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Format String Confusion (VB.NET 2003)

    Hi Chris,
    I'm not sure I see why you are trying to format a string value with a numeric format - you need to convert the string value to a number then format it - e.g.
    <pre>CDbl(model_value).ToString("#0.0#")</pre>

    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Mundelein, Illinois, USA
    Posts
    198
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Format String Confusion (VB.NET 2003)

    Thanks, that works.

    I used Format because that's what I found first that looked like it should work. It doesn't specify any specific input type in the documentation I'm using (Microsoft Visual Studio .NET 2003 Documentation), it just says "Any valid expression". I expected it would typecast to whatever was needed.
    Thanks,

    C DeWitt

  4. #4
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Format String Confusion (VB.NET 2003)

    You could also use:
    <pre>Format(CDbl(model_value), "#0.0#")</pre>

    If you look at the settings for the Style argument in Help you will notice that it does not show you how to format strings (since returning a string representation of a string is fairly pointless! <img src=/S/grin.gif border=0 alt=grin width=15 height=15>) but I confess that I would also expect an implicit conversion to be done, for exactly that reason. Obviously I haven't seen the rest of your code but if model_value was a number at some point before being converted into a string, it might make sense to either format it then or have an additional variable to hold the string representation whilst keeping the numeric variable intact.
    Regards,
    Rory

    Microsoft MVP - Excel

  5. #5
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Mundelein, Illinois, USA
    Posts
    198
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Format String Confusion (VB.NET 2003)

    I guess I don't agree. A number (integer, double, long, single, decimal . . .) has no inherent formatting. It has no set number of decimal places, dollar signs, comas, you can
    Thanks,

    C DeWitt

  6. #6
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Format String Confusion (VB.NET 2003)

    That was kind of my point - you only use the format function because a number/date etc. has no inherent formatting. You are converting it to a string representation.
    Under Visual Basic Language Concepts in MSDN, it says:
    <hr>In Visual Basic .NET, the Format function does not convert strings to numbers for you. Thus, the first of the following two lines of code is invalid in Visual Basic .NET, while the second is valid:

    Format("1.234", "#.#") ' Displays "#.#".
    Format(CSng("1.234"),"#.#") ' Displays "1.234".
    <hr>
    An alternative is to declare a variable of type double (or single) and assign the string value to it at which point an implicit conversion should take place and you can then pass the new variable to the format function.
    Regards,
    Rory

    Microsoft MVP - Excel

  7. #7
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Format String Confusion (VB.NET 2003)

    Only if you don't have Option Strict On, Rory. If you do, there are NO implicit conversions and you have to use a conversion funtion or CType to recast the value you're assigning to the correct object type.
    Charlotte

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •