I'm going to rely on the saying that no question is a dumb question, but I have a slightly dumb one to ask.
EDIT:
Seems that this question has been asked and answered a few times on here already, though using titles I didn't come across when searching for duplicates. Here are some related posts:
- http://stackoverflow.com/questions/1546113/double-to-string-conversion-without-scientific-notation
- http://stackoverflow.com/questions/1319191/how-to-convert-double-to-string-without-the-power-to-10-representation-e-05
- Place with an answer (Jon Skeet): http://www.yoda.arachsys.com/csharp/DoubleConverter.cs
Original Question:
I've got an app that sometimes stores some decently small numbers, such as 0.000023425
. These numbers get loaded up in TextBox
controls for users to edit. The following contrived example shows my issue:
Dim number As Double = 0.000023425
someTextBox.Text = number.ToString() ' Shows "2.3425E-05"
As mentioned, the text that shows is 2.3425E-05
, which isn't exactly intuitive for the user to see, plus I have numbers even more precise (out to 19 decimal places). I would like the output to be fixed point. Of course I could easily and cleanly do:
number.ToString("F20") ' Shows "0.00002342500000000000"
But then there's an annoying number of zeros left over. So, to fix that, I could do:
number.ToString("#,##0.####################") ' Shows "0.000023425"
Which is what I want, but is there any better way to do it than to have that giant ugly format string there? So, ok, it's not that ugly, but surely there's a different way, right? My goal is to show the raw value being stored in the DB in a friendly way, and I would rather not have to force a format on the number at all.
Thanks!
UPDATE
Perhaps I said a bit too much in the post. After some experimentation, I found that changing the underlying datatype to Decimal
solves the issue.
Dim number As Decimal = 0.000023425
someTextBox.Text = number.ToString() ' Shows "0.000023425" instead of "2.3425E-05"
So it sounds like Double
s aren't precise enough to be displayed normally?
EDIT
I found a post (albeit with no upvotes) that simply casts the the Double value as a Decimal before doing a .ToString()
on it, which has the desired effect. Is there any reason why I wouldn't want to do that? Perhaps the cast to Decimal
has the potential to end with a different value than the Double
(even though it's a negligible amount), so perhaps the #,##0.#################...
format string is safer.