I have a problem: a number is showing in scientific notation if it has 8 or more digits before the decimal point.
Is there a simple way to convert this number to decimal via a library or something?
I began creating a manual method to parse it out, but it seems overcomplicated.
Any help will be appreciated.
input example: 1.0225556677556E7
Edit: I also need to be able to identify a number that is in scientific notation
You can use NumberFormat your accomplish your goal easily.
String scientificNotation = "1.0225556677556E7";
Double scientificDouble = Double.parseDouble(scientificNotation);
NumberFormat nf = new DecimalFormat("################################################.###########################################");
decimalString = nf.format(scientificDouble);
To answer you're other question about matching scientific notation strings- you can use a regex and String.matches(). This one isn't perfect although the probability of getting false positives should be very low:
if(myString.matches("-?[\\d.]+(?:E-?\\d+)?")){
//do work
}
Related
I have a sequence of numbers like this:
1.687155E21
3.981457E19
0.5532155E21
3.018843E21
2.0532155E21
4.5532155E21
3.1637913E19
My problem is how to convert the two numbers which ends with 10^19 to be like the others (10^21). Because after this unification i need to trunc the number to print only something like 3.5.
In C/C++ i know how to work with precision, but in Java I haven't got any idea.
Divide all your number by / 1e19, round to as many decimal digits you want:
168.7155
3.981457
55.32155
301.8843
205.32155
455.32155
3.1637913
Use the Formatter Class to bring them into the desired scientific notation (java.util.Formatter)
I'd suggest something similar as Tomasz Nurkiewicz did, but instead of dividing by 1E19 divide by 1E21, convert them to strings with the required precision using Formatter (see the comment of count0) but not as scientific format, but as a general one. In the end just add E21 to those strings. In the end you should get (I hope, I got the idea correctly)
1.687155E21
0.03981457E21
0.5532155E21
3.018843E21
2.0532155E21
4.5532155E21
0.031637913E21
Can't you just multiply the E19 numbers by 10 ^ 2 = 100?
After they have been normalized to E21, you should be able to divide all of them by 10^21 (if they're floats), and they will all be in the range of 0-9.999...
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to round a number to n decimal places in Java
I am having difficulties rounding a float to two decimal places. I have tried a few methods I have seen on here including simply just using Math.round(), but no matter what I do I keep getting unusual numbers.
I have a list of floats that I am processing, the first in the list is displayed as 1.2975118E7. What is the E7?
When I use Math.round(f) (f is the float), I get the exact same number.
I know I am doing something wrong, I just am not sure what.
I just want the numbers to be in the format x.xx. The first number should be 1.30, etc.
1.2975118E7 is scientific notation.
1.2975118E7 = 1.2975118 * 10^7 = 12975118
Also, Math.round(f) returns an integer. You can't use it to get your desired format x.xx.
You could use String.format.
String s = String.format("%.2f", 1.2975118);
// 1.30
If you're looking for currency formatting (which you didn't specify, but it seems that is what you're looking for) try the NumberFormat class. It's very simple:
double d = 2.3d;
NumberFormat formatter = NumberFormat.getCurrencyInstance();
String output = formatter.format(d);
Which will output (depending on locale):
$2.30
Also, if currency isn't required (just the exact two decimal places) you can use this instead:
NumberFormat formatter = NumberFormat.getNumberInstance();
formatter.setMinimumFractionDigits(2);
formatter.setMaximumFractionDigits(2);
String output = formatter.format(d);
Which will output 2.30
You can make use of DecimalFormat to give you the style you wish.
DecimalFormat df = new DecimalFormat("0.00E0");
double number = 1.2975118E7;
System.out.println(df.format(number)); // prints 1.30E7
Since it's in scientific notation, you won't be able to get the number any smaller than 107 without losing that many orders of magnitude of accuracy.
Try looking at the BigDecimal Class. It is the go to class for currency and support accurate rounding.
Java comes up with numbers like 9.870699812169277E-4
How should I interpret it? Is there a way to parse it in Java and display without that E?
You can use NumberFormat.
Code
// you can format to any output you want
NumberFormat formatter = new DecimalFormat("0.00000000000");
String string = formatter.format(9.870699812169277E-4);
System.out.println(string);
Result
0.00098706998
Related
Java: Format double with decimals and
Format numbers in java
I don't know of any language which doesn't support this notation (except perhaps machine code) Even most calculators support it.
I suspect the languages you have used before support this notation, however it just wasn't used.
9.870699812169277E-4 is the same 9.870699812169277 * 10-4 or 0.0009870699812169277
For your interest there is a P notation e.g. 0x1.fffffffffffffP+1023 which is a hex notation for a double.
I am currently writing a calculator application. I know that a double is not the best choice for good math. Most of the functions in the application have great precision but the ones that don't get super ugly results. My solution is to show users only 12 decimals of precision. I chose 12 because my lowest precision comes from my numerical derive function.
The issue I am having is that if I multiply it by a scaler then round then divide by the scaler the precision will most likely be thrown out of whack. If I use DecimalFormat there is no way to show only 12 and have the E for scientific notation show up correctly, but not be there if it doesn’t need to be.
for example I want
1.23456789111213 to be 1.234567891112
but never
1.234567891112E0
but I also want
1.23456789111213E23 to be 1.234567891112E23
So basically I want to format the string of a number to 12 decimals places, preserving scientific notation, but not being scientific when it shouldn't
Use String.format("%.12G", doubleVariable);
That is how you use format() to display values in scientific notation, but without the scientific notation if not needed. The one caveat is that you end up with a '+' after the 'E', so yours would end up like 1.234567891112E+23
String.format("%.12d", doubleVariable);
Should give you what you are looking for in your first matter. I'm sorry but I don't know how to define when your E-notification is showed.
You'll be interested in BigDecimal, for example:
BigDecimal number = new BigDecimal("1.23456789111213");
number = number.setScale(12, RoundingMode.HALF_UP);
System.out.println(number);
Choose appropriate to you RoundingMode.
I want to implement or use some library for an intelligent decimal cut off.
I mean that I would like to get: from 3.456432 -> 3.4, from 0.0000023232432 -> 0.000002 and from 0.000000000001 -> 0.0 (or something like that). I need this feature for a convinient user GUI.
Thereby I need to reduce number of digits that are not equal to zero. I need to keep 1-3 most significant digits and other set to zero.
Have you taken a look at the DecimalFormat API?
DecimalFormat is a concrete subclass
of NumberFormat that formats decimal
numbers. It has a variety of features
designed to make it possible to parse
and format numbers in any locale,
including support for Western, Arabic,
and Indic digits. It also supports
different kinds of numbers, including
integers (123), fixed-point numbers
(123.4), scientific notation (1.23E4),
percentages (12%), and currency
amounts ($123). All of these can be
localized.
If it is of any help, you can use the following method to round a double to a specified number of significant digits. There are however no functionality in the standard API to output the result in a reasonable manner:
private static double round(double v, int sigDigits) {
double f = Math.pow(10, Math.ceil(Math.log10(Math.abs(v))) - sigDigits);
return Math.round(v/f)*f;
}
Since Java 5, java.util has a Formatter class which can do what you need.