Java parse a number in exponential notation
Use Double.valueOf()
and cast the result to long
:
Double.valueOf("9.18E+09").longValue()
Convert exponential values in a String into a decimal representation without exponential notation
Something like this should do the job:
public static void main(String[] args) {
String patternStr = "[0-9.-]*e[0-9.-]*";
String word = "m -263.61653,-131.25745 c -7.5e-4,-1.04175 0.71025,-1.90875 1.67025,-2.16526";
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(word);
if (matcher.find()) {
Double d = Double.valueOf(matcher.group());
System.out.println(word.replaceAll(patternStr, BigDecimal.valueOf(d).toPlainString()));
}
}
Output will be:
m -263.61653,-131.25745 c -0.00075,-1.04175 0.71025,-1.90875 1.67025,-2.16526
Of course if there are multiple exponentials on the String you will have to tweak it a bit.
How do I print a double value without scientific notation using Java?
You could use printf()
with %f
:
double dexp = 12345678;
System.out.printf("dexp: %f\n", dexp);
This will print dexp: 12345678.000000
. If you don't want the fractional part, use
System.out.printf("dexp: %.0f\n", dexp);
0 in %.0f
means 0 places in fractional part i.e no fractional part. If you want to print fractional part with desired number of decimal places then instead of 0 just provide the number like this %.8f
. By default fractional part is printed up to 6 decimal places.
This uses the format specifier language explained in the documentation.
The default toString()
format used in your original code is spelled out here.
Converting exponential into number in java
Try a NumberFormat
:
System.out.println(new DecimalFormat("#").format(yourLongNumber));
(You should not create a new format for each output, so reuse it when possible.)
Conversion of double to string gives exponent values
If I understood your problem correctly then you do not need String.valueOf()
but String.format()
.
Here is the code snippet:
public static void main (String[] args) throws Exception
{
String input = "32,646,513.32";
double value = Double.parseDouble(input.replace(",",""));
String output = String.format("%f",value);
System.out.println("Value: " + output);
}
Output:
Value: 32646513.320000
Replace the following line in your code appropriately:
/* Note the change from `valueOf()` to `format()` */
pvor.setAmount(String.format("%f",totalAMounts.doubleValue()));
Converting big decimal to double without exponential format
You seem to be confusing a double
, a 64 bit value where different bits have a different meaning, with its string representation.
Let's take a value of 0.015625
. Internally, the double is represented as 64 bits (I don't know right now which combination of bits, but that is irrelevant anyway).
But the string representation depends on the format settings of the library you are using. Note that the representations 0.015625
and 1.5625E-2
represent the exact same double value. The double is not stored as a string, it does not contain a .
or an E
, just a number of bits. The combination of these bits form its real value, and it is the same, no matter how you represent it as a string. If you have a different format setting, the result can just as well be 1.56E-2
. This just means that the code that converts the internal bit combination of the double
to a string does some rounding.
So, to obtain a double
from a BigDecimal
, simply use bd.doubleValue()
. There is no need to use an intermediate string representation, and it can even be detrimental to do so, because if the string representation performs some rounding, you don't get the best approximation of the value in the BigDecimal
.
So again, bd.doubleValue()
is the only correct way to do this. This does not return a specific format, it simply returns the double
that is closest to the value in the BigDecimal
.
And again, do not confuse the double
type with how it is represented as a string. These are different things. A double
is a double
is a double
, and that is not stored as a string, exponential or plain or rounded or whatever.
Actually, BigDecimal.toString()
and BigDecimal.toPlainString()
also return different string representations of the same BigDecimal
. AFAIK, there is even a third string conversion function, toEngineeringString()
, which gives you yet another string representation of the same BigDecimal
. This is similar for double
: different output routines with different arguments return different strings for the exact same value.
Related Topics
How to Resolve Java.Lang.Noclassdeffounderror: Javax/Xml/Bind/Jaxbexception
Parse Xml to Java Pojo in Efficient Way
The Import Org.Junit Cannot Be Resolved
How to Create Comma Separated String in Single Quotes from Arraylist of String in Java
How to Replace Dot (.) in a String in Java
Eclipse No Tests Found Using Junit 5 Caused by Noclassdeffounderror for Launcherfactory
Kafka: How to Delete Records from a Topic Using Java API
Java.Time.Format.Datetimeparseexception: Text Could Not Be Parsed At Index 3
Java.Lang.Noclassdeffounderror: Com/Fasterxml/Jackson/Databind/Exc/Invaliddefinitionexception
Adb Cannot Connect to Daemon At Tcp:5037
How to Refresh or Reload the Jframe
Split Comma Separated Values in Java, Int and String
Password Validate 8 Digits, Contains Upper, Lowercase, and a Special Character
Spring Security Configuration Filter Any Requests Except a Specific Endpoint
How to Sort Json Object in Java