Parse a Number from Exponential Notation

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);

Java parse a number in exponential notation

Use Double.valueOf() and cast the result to long:

Double.valueOf("9.18E+09").longValue()

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)

Parse complex numbers with exponential notation Java

Your data seems to contain the scientific notation with a small e. It is annoying that the library does not intercept but a simple replace will help in your case.

String entry = "-7.5212e-06+3.4298e-06i";
ComplexFormat cf = new ComplexFormat();
Complex c = cf.parse(entry.replace('e', 'E'));
System.out.println(c);
//(-0.075212, 0.034298)

But note if you use the default constructor (as above) your numbers will be parsed correctly but the scientific notation will not be maintained anymore. if you want to keep the notation, use DecimalFormat:

DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
DecimalFormat formatter = new DecimalFormat("##.####E0", symbols);
ComplexFormat cfWithE = new ComplexFormat(formatter);
Complex c2 = cfWithE.parse(entry.replace('e', 'E'));
System.out.println(c2);
//(-7.5212E-6, 3.4298E-6)

parsing integer in exponential notation from string

You can use stod (see docs) to do this, by parsing it as a double first. Be wary of precision issues when casting back though...

#include <iostream>   // std::cout
#include <string> // std::string, std::stod

int main () {
std::string text ("1e3");
std::string::size_type sz; // alias of size_t
double result = std::stod(text,&sz);
std::cout << "The result is " << (int)result << std::endl; // outputs 1000
return 0;
}

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);

Converting number in scientific notation to int

Behind the scenes a scientific number notation is always represented as a float internally. The reason is the varying number range as an integer only maps to a fixed value range, let's say 2^32 values. The scientific representation is similar to the floating representation with significant and exponent. Further details you can lookup in https://en.wikipedia.org/wiki/Floating_point.

You cannot cast a scientific number representation as string to integer directly.

print int(1e1)  # Works

Works because 1e1 as a number is already a float.

>>> type(1e1)
<type 'float'>

Back to your question: We want to get an integer from float or scientific string. Details: https://docs.python.org/2/reference/lexical_analysis.html#integers

>>> int("13.37")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '13.37'

For float or scientific representations you have to use the intermediate step over float.



Related Topics



Leave a reply



Submit