Java and decimals E numbers - java

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.

Related

How do I comma separate zero padded String.format doubles in java?

I am trying to use String.format for get comma separated, zero padded doubles.
I am using:
"%0,9.2f", unitPrice
Unit price is a double. When unitPrice is, for example, 44.99 I get 000044.99 when I want 00,044.99. If unit price is 4499 instead I get 04,499.00 as wanted.
You could use a DecimalFormat (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) like
DecimalFormat df = new DecimalFormat("00,000.00");
System.out.println(df.format(44.99));
System.out.println(df.format(4499));
Output is (as requested)
00,044.99
04,499.00
This is only possible for values equal or larger than 1000. Checked the source code of Java 8 and the zero padding is done after inserting group separators.
You have to write your own format like the answer from #Elliot Frisch.
Note that his answer is Locale dependent, but you can force the comma , by providing a localized instance of DecimalFormatSymbols.
new DecimalFormat("00,000.00", DecimalFormatSymbols.getInstance(Locale.US))

Convert a number from scientific notation to decimal in JAVA

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
}

convert BigDecimal to Scientific Notation Java

How do i print this in scientific notation:
BigDecimal r= (a.subtract(exact, MathContext.DECIMAL128)).divide(exact, MathContext.DECIMAL128).stripTrailingZeros();
DecimalFormat format = new DecimalFormat("0.###E0");
System.out.println(new BigDecimal(format.format(r));
where:
a = 1.111111111111111160454356650006957352161407470703125
exact = 0.11
returns:
r = 0.900010000000000004
any ideas? I've also tried calling EngineeringString() on the BigDecimal but this also didn't seem to work for me
You overdid the thing. What you only need is:
System.out.println(format.format(r));
The DecimalFormat object does indeed create a string, creating a BigDecimal instance again would just parse the number from string again - and the toString() method is called on the BigDecimal instance, produing the output you described...
Some clarification
BigDecimal, and other numeric formats (and dates too!) are stored in binary formats in the memory, abstracted from how us, humans think of them. BigDecimal for example stores the decimal digits, and where the decimal point is. Floating point numbers are even more sophisticated. Date stores the seconds from The Epoch. You need to format them to be readable. Formatting means to create a String (or semantically similar) object, that represents the value of the given object in the desired format. This doesn't involve changing the original object in any way.
The default formatting, toString() provides one generic format. To get your output the way you'd like does not mean to change the value to be formatted right with toString(), but to transform the unchanged value into the right String. Nice example is Double.toString() (using sun.mic.FloatingDecimal): it does exponential notation when the number is large or small enough, but in between, it prints in plain decimal format...

I need to round a float to two decimal places in Java [duplicate]

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.

How to do intelligent decimal cut off in Java?

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.

Categories