views:

398

answers:

2

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. http://stackoverflow.com/questions/1546113/double-to-string-conversion-without-scientific-notation
  2. http://stackoverflow.com/questions/1319191/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.

+3  A: 

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.

Godeke
Perhaps I said a little too much in my post. The simple question is: How would I __convert__ `2.3425E-05` to `0.000023425` without shoving it through a formatter (i.e. the number is clearly not out of range for a normal `double` value, so why can't it just show me the number as `0.000023425`?)?
Cory Larson
See my __update__ and __edit__ in my post.
Cory Larson
Obviously what you have found is simpler. Double is not as "precise" as Decimal (which is designed for dealing with decimal currency amounts without binary rounding issues). If you only cast on display, I suspect in most cases it will work. However, double does suffer from problems if you do computations where you get 0.9999999 type results instead of 1.0 . If this isn't a problem, the casts would be sufficient. Still, having a "percent" and "currency" dedicated control takes a lot of guesswork and redundancy out of the issue.
Godeke
To answer the "why" question... the default formatter (which is what ToString() uses) happens to flip to scientific outside a fairly narrow range. That is why you have to use an explicit format to get what you want. (You aren't actually converting from one thing to another, they are simply different output representations of the same thing.)
Godeke
A: 

Did u mean something like number.ToString("F09")

madan
I have that as an example. My goal is to not have to format the number to make it display correctly.
Cory Larson