How to convert the "6020494385.89982" string value to a double?
String ss = "6020494385.89982";
double dd = Double.parseDouble(String.valueOf(ss));
System.out.println(dd);
I am getting the output as
6.02049438589982E9 but I want 6020494385.89982 as double value.
Use of a DecimalFormatobject should do the trick
String ss = "6020494385.89982";
double dd = Double.parseDouble(String.valueOf(ss));
System.out.println(new DecimalFormat("#0.00000").format(dd));
Or you can use System.out#printf()
System.out.printf("%.5f", dd);
Because System.out.println has a particular way of working with double precisions that will not work in this particular case for example.
The number is correct. 6.02049438589982E9 is just the scientific notation, which is the default when you print a double. If you want the non-scientific notation, you could just use printf instead of println:
System.out.printf("%f\n", dd);
6.02049438589982E9 is the same as 6020494385.89982
6.02049438589982E9 = 6.02049438589982 X 10^9
the value stored in the double is 6020494385.89982 However, when you print it out, it represents in this format: 6.02049438589982E9
You do not need to convert into double if in the end you want the representation in the same format as the current value of ss variable.
However if you need to convert for some other purpose and then need to show in that format later/else where you can use DecimalFormat as others have suggested.
Related
Double d = 10.30
When I use this value of d i.e 10.30 it becomes 10.3
Is there anyway that I can sustain this value of 10.30
Remember I cannot change the data-type it should be a double
Is there a way??
You can't do this with Double class.
If you want to display a value with zero you could do this:
System.out.printf("%.2f%n", d);
It means that your variable d will be displayed with 2 digits after point. So, if d = 10.3, the code above will show 10.30
String also can store formatted value:
String s = String.format("%.2f%n", d);
System.out.println(s);
A double only contains only information about the value it represents, but not about the precision of it.
Whether a double is represented as 10.3, 10.30, 10.300000 etc. is determined by the display resp. string conversion routine, not by the routine which generates the value.
The best solution is
private static final DecimalFormat df = new DecimalFormat("0.00");
df.format(amountDouble)
For example; I have the next Double value: Double number = 53149000;
And I want to display it like this, with a format: 53,149
Is this possible...?
Thanks in Advance...
You can use the String.format method after dividing your value by 1000
double number = 53149000;
System.out.println(String.format("%,.0f", number/1000));
I suggest you use the DecimalFormat object to achieve that. For example:
DecimalFormat decimalFormat = new DecimalFormat("###,###");
System.out.println(decimalFormat.format(number/1000));
It prints 53,149 as expected.
I hope it helps.
i want to print a double value like 0.000027,
but it's getting printed as 2.7340E-05.
i'm using-
editTextYARD.setText(String.format(Locale.US, "%6.4E"));
please help...
This will convert a given double value to show decimal point up to 7
double value = 0.0000027;
System.out.println(String.format("%.7f", (double)value));
you may use it like this
editTextYARD.setText(""+String.format("%.7f", (double)value));
You can use DecimalFormat for this purpose.
double myvalue = 1.2345678878;
DecimalFormat df = new DecimalFormat("#");
df.setMaximumFractionDigits(10);
your_editText.setText(df.format(myvalue));
When print to console:
System.out.printf( "%04.2f", var)
When print to string, I think ( I am not tried yet...)
This worth trying..
String ret = String.format ("%04.2f", var)
I have this code:
Long dval = new Long((new Date()).getTime());
System.out.println("ogval:"+dval);
Double dd = (double)dval;
System.out.println("dval:"+dd);
Here is the output:
ogval:1381490769636
dval:1.381490769636E12
When I convert the value to Double, it adds a decimal point. Can I do the typecasting and get the value in double as it is?
The desired output would be:
ogval:1381490769636
dval:1381490769636
I have a function whose argument accepts only double value. When I try to pass a timestamp, it passes the decimal value inside the method.
I can't edit the function because its an inbuilt function of some package.
Simple answer is no.
Floating types can contain integer up to some arbitrary value, given by the way floats are stored. If the number is too big, it gets converted to decimal.
If you need to work with big integer values use BigInteger class.
Great tool to examine those imperfections is this float converter.
Try 123456789 in the float converter, it won't be stored exactly.
Use DecimalFormat, like:
Long dval = new Long((new Date()).getTime());
System.out.println("ogval:" + dval);
Double dd = (double) dval;
DecimalFormat format=new DecimalFormat("##########");
System.out.println("dval:" + format.format(dd));
Your problem is not with the type that you are using, but with the format that you are applying to it. Currently, the default format is used, because string + double implicitly calls Double.toString, which converts your specific double to a String using scientific notation. You can force a different format if you wish by using printf or any other formatting method that Java makes available to you:
System.out.printf("dval: %12.0f", dd);
(demo)
as an alternative you can try using bigdecimal
Long dval = new Long((new Date()).getTime());
System.out.println("ogval:"+dval);
Double dd = (double)dval;
System.out.println("dval:"+dd);
BigDecimal bd = new BigDecimal(dval);
System.out.println("bdval:"+bd.toPlainString());
I have a very small number and I want to convert it to a String with the full number, not abbreviated in any way. I don't know how small this number can be.
for example, when I run:
double d = 1E-10;
System.out.println(d);
it shows 1.0E-10 instead of 0.000000001.
I've already tried NumberFormat.getNumberInstance() but it formats to 0. and I don't know what expression to use on a DecimalFormat to work with any number.
Assuming that you want 500 zeroes in front of your number when you do:
double d = 1E-500;
then you can use:
double d = 1E-10;
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(Integer.MAX_VALUE);
System.out.println(nf.format(d));
You can set the maximum and minimum number of digits in the fraction of a numberformatter with setMinimumFractionDigits and setMaximumFractionDigits. that should fix the problem.
You can do it with BigDecimals in Java 5 using:
System.out.println(new java.math.BigDecimal(Double.toString(1E-10)).stripTrailingZeros().toPlainString());
Note that if you have the double value as a String in the first place, you would be better off using:
System.out.println(new java.math.BigDecimal("1E-10").toPlainString());
... as explained in the BigDecimal javadocs.
Have a look at: http://java.sun.com/docs/books/tutorial/i18n/format/decimalFormat.html#numberpattern
I think the format "0.####" might work.