• Format String Confusion (VB.NET 2003)

    Home » Forums » Developers, developers, developers » DevOps Lounge » Format String Confusion (VB.NET 2003)

    Author
    Topic
    #411825

    I have a piece of code where I print using

    Viewing 1 reply thread
    Author
    Replies
    • #895647

      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.

      CDbl(model_value).ToString("#0.0#")

      Hope that helps.

      • #895704

        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.

        • #896018

          You could also use:

          Format(CDbl(model_value), "#0.0#")

          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! grin) 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.

          • #896077

            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

            • #896123

              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:
              [indent]


              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”.


              [/indent]
              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.

            • #896203

              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.

            • #896204

              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.

            • #896124

              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:
              [indent]


              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”.


              [/indent]
              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.

          • #896078

            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

        • #896019

          You could also use:

          Format(CDbl(model_value), "#0.0#")

          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! grin) 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.

      • #895705

        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.

    • #895648

      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.

      CDbl(model_value).ToString("#0.0#")

      Hope that helps.

    Viewing 1 reply thread
    Reply To: Reply #896019 in Format String Confusion (VB.NET 2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel