Tech Tock

Time is of the essence.

Wonderful World of ToString

Interesting experience reading a text file and converting strings to numbers.  It seems the standard string converter is subject to rounding.  You can see the details on msdn.

It’s not immediately obvious from the docs, but here’s what I found in the Immediate window:

I had numbers that represented dates:

(Single) 20091022

The standard ToString was in exponential format, that was the first unexpected result:

((Single) 20091022).ToString()

When using a formatting specifier, the last digit was cut off:

((Single) 20091022).ToString("#")

How and why this relates to the culture setting as per the docs is beyond me.  My personal culture says that numbers before the decimal point are significant.  It has the same precision as the exponential format.

To fix the problem, I chose the “r” specifier.  R is for round trip, what you get can be converted back to what you had:

((Single) 20091022).ToString("r")

November 3, 2009 - Posted by | Uncategorized | , ,


  1. >> I had numbers that represented dates

    I think that’s your first problem. Just because it looks like a number, doesn’t mean it should be stored as a number. It’s designs like that which leads to me getting mail with zip code “7003” (it should be “07003”)

    Comment by James Curran | November 4, 2009 | Reply

  2. Yes, the article doesn’t state the reason for my folly, but there was a good reason at the time.

    That’s my story and I’m stickin’ to it.


    Comment by goldmanalpha | November 4, 2009 | Reply

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: