i want to round a double variable into two decimals only [duplicate] - java

This question already has answers here:
Round a double to 2 decimal places [duplicate]
(13 answers)
Closed 8 years ago.
how can i round a double variables in java into only two decimals?

yourNewFormatedDouble = new DecimalFormat("#.##").format(yourOldDoubleHere);

DecimalFormat df = new DecimalFormat("0.00##");
String result = df.format(myDouble);

Related

Why doesn't Math.pow return me the correct value? [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 2 years ago.
I am trying to get the correct value for:
double poweredValue = Math.pow((1.1),(1 / 365));
Using my scientific calculator I get 1.0002611578760678121616866817054‬. But Math.pow returns a 1.
How do I get the full value returned?
double poweredValue = Math.pow((1.1),(1 / 365.0));
System.out.println(poweredValue);
Output -
1.0002611578760678

how to convert the double value to n places in java (giving the answer ,not asking the question) [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Round a double to 2 decimal places [duplicate]
(13 answers)
round up to 2 decimal places in java? [duplicate]
(12 answers)
Closed 3 years ago.
DecimalFormat df = new DecimalFormat("0.00");//if you want over four decimal places then do->"0.0000" in brackets
TextView lattextview=findViewById(R.id.lattextview);
TextView lontextview=findViewById(R.id.longtextview);
TextView alttextview=findViewById(R.id.alttextview);
TextView accuracytextview=findViewById(R.id.accuracytextview);
TextView addresstextview=findViewById(R.id.addresstextview);
String s1 = df.format(location.getLatitude());
String s2 = df.format(location.getLongitude());
String s3 = df.format(location.getAltitude());
String s4 = df.format(location.getAccuracy());
lattextview.setText("Latitude : "+s1);
lontextview.setText("Longitude : "+s2);
alttextview.setText("Altitude : "+s3);
accuracytextview.setText("Accuracy : "+s4);

how to set 3 range zero in double in android java [duplicate]

This question already has answers here:
How do I print a double value without scientific notation using Java?
(18 answers)
Closed 4 years ago.
When I enter the number 0.0007575 on the double, the result will be "7.575E-4", but I want result 0.0007575
thanks
Use a custom DecimalFormat to format the double value:
DecimalFormat fmt = new DecimalFormat("#0.000"); // leading zero and 3 fraction digits
String result = fmt.format(11.123456789);
System.out.println(result); // 11.123

I want to print amount like 2523525252.025 when I using double data type it's changed in 2.523525252025E9 how can fix? [duplicate]

This question already has answers here:
How do I print a double value without scientific notation using Java?
(18 answers)
Closed 5 years ago.
I want to print amount like 2523525252.025 when I using double data type it's changed in 2.523525252025E9 how can fix?
Example -
Double saleprice=145236.12;
int itemqountity=1500;
Double totalamount= saleprice*itemqountity;
Log.e("totalamount",""+totalamount);
output-
E/totalamount: 2.1785418E8
Please use DecimalFormat to print your value without scientific notation here is an example.
double test = 12345678;
DecimalFormat df = new DecimalFormat("#");
df.setMaximumFractionDigits(0);
System.out.println(df.format(test));
//12345678
BigDecimal bigDecimal = new BigDecimal(2.523525252025E9);
System.out.println(bigDecimal.toPlainString());

Rounding to one decimal place? [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 5 years ago.
I have the following variables initialized to the user inputs,
int completedPasses = reader1.nextInt();
int attemptedPasses = reader2.nextInt();
double completionRatio = (250 / 3) * ((completedPasses / attemptedPasses) - 0.3);
How do I round the computation above to a double of one decimal place?
Use DecimalFormat:
DecimalFormat df = new DecimalFormat("#.#");
df.format(completionRatio);
Or Math.round:
Math.round(completionRatio);

Categories

Resources