Convert from scientific notation string to float in C#
Double.Parse("1.234567E-06", System.Globalization.NumberStyles.Float);
Parse a Number from Exponential Notation
It is a floating point number, you have to tell it that:
decimal d = Decimal.Parse("1.2345E-02", System.Globalization.NumberStyles.Float);
Convert number in scientific notation string to float with limited C-like library
In CAPL the function is called atodbl
with the same signature as C's atof
.
How to Convert Positive or Negative Scientific Notation to Number in C#?
If your culture uses "." as separator:
decimal d = Decimal.Parse("-8.13E-06", System.Globalization.NumberStyles.Float);
Or you can specify the InvariantCulture
:
decimal d = Decimal.Parse("-8.13E-06", System.Globalization.NumberStyles.Float, CultureInfo.InvariantCulture);
or as in your exapmple:
Decimal h2 = 0;
Decimal.TryParse("-8.13E-06", NumberStyles.Float, CultureInfo.InvariantCulture, out h2);
C#: Scientific notation String to Int64 conversion failing
I'm going to assume that this is a bug with the version of Mono C# I'm using, which comes with Unity 5.5.x or earlier. Their repository can be found here.
Their implementation of Int64.Parse
does not even check for the NumberStyles.AllowExponents
flag, or handle exponents in any way. So it's going to fail when it finds the +
symbol in the string. Basically, Int64.Parse
when using Unity does not support exponents.
Mono's Int32.Parse
does seem to look for exponents, but still causes an OverflowException with all exponents that I give it.
Decimal.Parse
actually does work with the same parameters as the other two, which suggests there was nothing wrong with the string or parameters, but it's just a bug in their other Parse methods. Decimal's parsing is completely different from how the Int parsing is being done, so that may explain why it works and the others don't.
Convert numbers with exponential notation from string to double or decimal
If your culture uses .
as the decimal separator, just double.Parse("1.50E-15")
should work.
If your culture uses something else (e.g. ,
) or you want to make sure your application works the same on every computer, you should use InvariantCulture
:
double.Parse("1.50E-15", CultureInfo.InvariantCulture)
Converting String To Float in C#
Your thread's locale is set to one in which the decimal mark is "," instead of ".".
Try using this:
float.Parse("41.00027357629127", CultureInfo.InvariantCulture.NumberFormat);
Note, however, that a float cannot hold that many digits of precision. You would have to use double or Decimal to do so.
How to convert this scientific notation to a decimal form using C#
You can modify the string representation of the result by using ToString(string)
with your desired format string. See MSDN.
In your view, this will appear as @Model.result.ToString("<format string>")
.
Related Topics
Badimageformatexception When Loading 32 Bit Dll, Target Is X86
Center Multiple Rows of Controls in a Flowlayoutpanel
Programmatic Control of Virtual Desktops in Windows 10
Remove File Extension from a File Name String
Assign Format of Datetime with Data Annotations
Write Values in App.Config File
Memorystream.Close() or Memorystream.Dispose()
Get Individual Query Parameters from Uri
What Is the Use of 'Abstract Override' in C#
Initial Capacity of Collection Types, E.G. Dictionary, List
Difference Between Delegate.Invoke and Delegate()
Store a Reference to a Value Type
How to Automatically Display All Properties of a Class and Their Values in a String