Easier way to prevent numbers from showing in exponent notation

13,721

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.

Share:
13,721
Cᴏʀʏ
Author by

Cᴏʀʏ

I work at Corporate Technology Solutions, Inc., a software consulting firm that specializes in custom software development for the financial services industries in the Greater Milwaukee Area in Wisconsin. I earned a Bachelor of Science degree in Software Engineering from Milwaukee School of Engineering. As a consultant and engineer, typical projects for me revolve around full-stack web application development. My strengths include: gathering functional and technical requirements, database design and administration (ssis, sql-server), development (c#, vb.net, aspnet-mvc, sql, javascript, jquery and more), testing, integration, and training. In my spare time I like to read, relax, sample great Wisconsin beer, and of course, contribute to Stack Overflow! Some achievements I'm proud of (Stack Overflow): Usually in the top 1,000 users for all time rank by reputation 38/40 candidate score (if I were to ever run for moderator) c# – gold badge jquery – gold badge javascript – silver badge sql – silver badge sql-server – silver badge I never intend to give bad advice. If you find that any of my answers are counter-productive or just plain wrong, please leave a comment! I'm more than happy to fix or delete answers if they aren't up to snuff.

Updated on June 26, 2022

Comments

  • Cᴏʀʏ
    Cᴏʀʏ almost 2 years

    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.

  • Godeke
    Godeke over 14 years
    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
    Godeke over 14 years
    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.)