-1

what is the best way to check if a double has an exponential notation? is there a way to do it without converting it to string?

my best solution so far is

var asString = doubleValue.ToString();
var containsExpNotation = asString.Contains('e') || asString.Contains('E')

thanks in advance :)

Eliot
  • 37
  • 5
  • 1
    That could work, but internally they all do. What's the real problem you're trying to solve? – 500 - Internal Server Error Sep 22 '21 at 19:00
  • 1
    You can use formatters to avoid that notation when formatting the value to a string. Otherwise you're basically just checking if the value has a very large or very small magnitude. – juharr Sep 22 '21 at 19:02
  • In fact, I want to decide whether to round a number whether it has an exponential notation (UI consideration, trying to avoid too long numbers) there may be a better solution but I need something simple for a quick fix – Eliot Sep 22 '21 at 19:03
  • @LiorSwisa Then the question is entirely based on how many digits your particular UI has space to render, which none of us can tell you. – Servy Sep 22 '21 at 19:04
  • Internally, a double is stored in a binary structure, not in scientific notation. It might be *expressed* in scientific notation when converted to a string (explicitly or implicitly). So I think the question you really want to ask it [when does `double.ToString()` return a value in scientific notation?](https://stackoverflow.com/questions/12977746/when-does-double-tostring-return-a-value-in-scientific-notation) – John Wu Sep 22 '21 at 19:05
  • 1
    @JohnWu Well, internally floating point numbers are in fact stored as an exponent and a mantissa (which is functionally scientific notation) although it's in base 2 not base 10. Not that the mechanism by which it's stored has anything to do with how it's rendered as a string, as you said. – Servy Sep 22 '21 at 19:08
  • 2
    To elaborate on @JohnWu's point. A `double` has no notation associated with it. It's only when it's rendered as a string that it has a _notation_. How it's rendered has nothing to do with how many significant figures it has or how large the exponent is. For example, `Math.PI.ToString("E2");` results in a string that looks like `"3.14E+000"` – Flydog57 Sep 22 '21 at 19:10

1 Answers1

1

Every floating point number can be written in scientific notation (using e or E). You can use the format arguments to specify whether you want that or not:

doubleValue.ToString(); // same as "G"
doubleValue.ToString("G"); // Format as floating point, unless more than 15 digits would be required, in which case scientific notation is used
doubleValue.ToString("E"); // Use scientific notation
doubleValue.ToString("F"); // Use floating point notation

So it's all in your hands whether a number is represented with an exponent or not.

See https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings for the full list of options.

PMF
  • 14,535
  • 3
  • 23
  • 49
  • from what I saw in the documentation G chooses the most compact way to write a number (so for example 0.00001 will be 1E-05, thanks! – Eliot Sep 22 '21 at 19:18
  • Yea, G is a bit tricky (you can also choose the number of digits). Most of the time, I use the "F" specifier together with a number, e.g. "F2", because I know that the value is within an useful range for this. – PMF Sep 22 '21 at 19:58