Results 1 to 5 of 5

Thread: [RESOLVED] String Format Change?

  1. #1

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Resolved [RESOLVED] String Format Change?

    According to the documentation seen here:

    https://docs.microsoft.com/en-us/dot...format-strings

    a string format of "D2" looks like it should be a decimal with two digits to the right of the decimal place...or something, anyways. However, "D2" results in an invalid format specifier exception, these days.

    I'm pretty sure it WAS working, but I've now had two programs fail because of this. Can anybody say when or why this changed?
    My usual boring signature: Nothing

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,398

    Re: String Format Change?

    D2 is supported by integral types only. So for example:
    Code:
    Dim value1 As Integer = 123456789
    Console.WriteLine(value1.ToString("D2")) ' works
    
    Dim value2 As Decimal = 1.23456789
    Console.WriteLine(value2.ToString("D2")) ' fails
    In the words of Obi-Wan Kenobi, these aren't the droids you're looking for.

    You actually want to use N2.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: String Format Change?

    Quote Originally Posted by Shaggy Hiker View Post
    According to the documentation seen here:

    https://docs.microsoft.com/en-us/dot...format-strings

    a string format of "D2" looks like it should be a decimal with two digits to the right of the decimal place...or something, anyways. However, "D2" results in an invalid format specifier exception, these days.

    I'm pretty sure it WAS working, but I've now had two programs fail because of this. Can anybody say when or why this changed?

    Decimal format specifier (D)

    The "D" (or decimal) format specifier converts a number to a string of decimal digits (0-9), prefixed by a minus sign if the number is negative. This format is supported only for integral types.

    The precision specifier indicates the minimum number of digits desired in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier. If no precision specifier is specified, the default is the minimum value required to represent the integer without leading zeros.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: String Format Change?

    HAHAHAHA!!

    I read the documentation wrong. I actually have been using N for a long time, and ended up misreading D...twice.
    My usual boring signature: Nothing

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [RESOLVED] String Format Change?

    You’re using the type specifier D. You could use C2 for currency, N2 (1,000,000.75 etc), or F2 (1000000.75 etc)…

    Code:
    Dim decimalVariable As decimal = 2.5D
    Last edited by .paul.; Mar 22nd, 2022 at 11:15 AM.

Posting Permissions

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



Click Here to Expand Forum to Full Width