Easier way to prevent numbers from showing in exponent notation

Cᴏʀʏ picture Cᴏʀʏ · Oct 21, 2009 · Viewed 12.7k times · Source

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:

  1. Double to string conversion without scientific notation
  2. How to convert double to string without the power to 10 representation (E-05)
  3. 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 Doubles 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.

Answer

Godeke picture Godeke · Oct 21, 2009

Make your format string a constant (say in your resource file) and use it by name. You avoid seeing the ugly format and gain consistency from control to control in your format (i.e., if you change your mind, all controls will gain the new look).

As an alternative (or in conjunction), derive a custom control from the text box and have properties that invoke the different formatting strings you wish to use in an easier syntax.

Either way your goal should be not repeat yourself... by placing the configuration in one place you avoid mistyping it eventually and having to track down a strange formatting bug.

We do something similar by creating "percent" and "currency" text controls that encapsulate all the formatting and parsing requirements. They work just like text controls otherwise.