You can use String.format("%.2f", d), your double will be rounded automatically.
Remember you can't use
String.format("%.2f", maltRequiredString);
Because maltRequiredString is string. Correct coding is that you must use float in this function and for that you have to convert your string to float
float f = Float.valueOf(maltRequiredString);
String test = String.format("%.02f", f);
You can also use this technique for making it 2 decimal
DecimalFormat decimalFormat = new DecimalFormat("#.##");
float twoDigitsF = Float.valueOf(decimalFormat.format(f));
You can use DecimalFormat to overcome this problem.
//Make a new instance df that has 2 decimal places pattern.
Decimalformat df = new DecimalFormat("#.##");
// converting maltRequired form double to string.
String maltRequiredString = df.format(maltRequired);
// then set the value of maltRequiredString to the TextView.
maltRequiredTextView.setText(String.valueOf(maltRequiredString));
yourTextView.setText(String.format("Value of a: %.2f", a));
For Displaying digit upto two decimal places there are two possibilities - 1) Firstly, you only want to display decimal digits if it's there. For example - i) 12.10 to be displayed as 12.1, ii) 12.00 to be displayed as 12. Then use-
DecimalFormat formater = new DecimalFormat("#.##");
2) Secondly, you want to display decimal digits irrespective of decimal present For example -i) 12.10 to be displayed as 12.10. ii) 12 to be displayed as 12.00.Then use-
DecimalFormat formater = new DecimalFormat("0.00");
DecimalFormat uses String (thus allocates additional memory), a big overhead compared to
(float)Math.round(value * 100) / 100
You can use the DecimalFormatobject, similar to regular Java.
Try
double roundTwoDecimals(double d)
{
DecimalFormat twoDForm = new DecimalFormat("#.##");
return Double.valueOf(twoDForm.format(d));
}
(code example lifted from http://www.java-forums.org/advanced-java/4130-rounding-double-two-decimal-places.html)
Have you tried this:
DecimalFormat sf = new DecimalFormat("00.000000");
String s = sf.format(5.494394);
System.out.println(s); //prints 05.494394
EDIT
Based on your new question, why don't you do this:
double latitude = location.getLatitude();
double longitude = location.getLongitude();
GeoPoint ourLocation = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
//....
System.out.println("Lat is " + latitude);
System.out.println("Longi is " + longitude);
Convert to String, add leading zero. StringFormatter might help. An integer will never have leading zeroes.
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
Here's what I think you mean
BigDecimal n = new BigDecimal("1.1111111111111");
n = n.setScale(2, BigDecimal.ROUND_CEILING);
As for the first question:
1.) I'd suggest you use the method explained in:
How to round a number to n decimal places in Java. It is well explained there.
And the second one:
2.) I would personally just round the final answer.
EDIT: After a lot of bugs as pointed by Dave this is the final code:
Instead of the statement:
double bill = Double.parseDouble(originalBill.getText().toString());
You need to write and add:
BigDecimal bill = new BigDecimal(originalBill.getText().toString());
BigDecimal tax = new BigDecimal("1.10");
BigDecimal billWithTax = modifiedBill.multiply(tax);
BigDecimal roundedBill = billWithTax.setScale(2,RoundingMode.HALF_UP);
And change the next line in your code to:
String billString = String.valueOf(roundedBill);
Also you will need to import
java.math.BigDecimal;
java.math.RoundingMode;
Also, do note that you would have to change the earlier declaration of billWithTax being a double.
Use the code below. This will give you a string in return with just 2 decimal places.
// Continuing from the code you have on thee link ...
double Udregning = aValue * EuroTilKroner;
DecimalFormat df = new DecimalFormat("#.00");
String s = df.format(Udregning);
result.setText(s);
Hope this helps else please comment.
Update
As @Oren nicely suggested, you may set the rounding mode as follows:
df.setRoundingMode(RoundingMode.HALF_EVEN);
For more modes see the link.
You can use String.format("%.2f", value), your double will be rounded automatically.
Since the variables n1, n2 and n3 are ints, their sum and subsequent division are integer as well. As such, the result is truncated to the integer.
To get the accurate result, you should force the division to be a floating-point one. To do this, one of the operands should be a double. You can force this by replacing 3 with 3.0
(n1 + n2 + n3) / 3.0
Next, is how can we round the result to two decimal digits. We can multiply the result by 100, perform the integer division and only then divide by 100 and convert to double.
((n1 + n2 + n3) * 100 / 3) / 100.0
You can see additional ways to round in this question.
The issue is with the following line of code
calc = (n1 + n2 + n3) / 3;
All values are integers so it is computing (11 + 22 + 10) / 3 as the integer value 14 by means of integer division.
If you change the divisor to a double the result will be floating point arithmetic.
calc = (n1 + n2 + n3) / 3.0;
Don't use doubles. You can lose some precision. Here's a general purpose function.
public static double round(double unrounded, int precision, int roundingMode)
{
BigDecimal bd = new BigDecimal(unrounded);
BigDecimal rounded = bd.setScale(precision, roundingMode);
return rounded.doubleValue();
}
You can call it with
round(yourNumber, 3, BigDecimal.ROUND_HALF_UP);
"precision" being the number of decimal points you desire.
Just use Math.round()
double mkm = ((((amountdrug/fluidvol)*1000f)/60f)*infrate)/ptwt;
mkm= (double)(Math.round(mkm*100))/100;
I was working with statistics in Java 2 years ago and I still got the codes of a function that allows you to round a number to the number of decimals that you want. Now you need two, but maybe you would like to try with 3 to compare results, and this function gives you this freedom.
/**
* Round to certain number of decimals
*
* @param d
* @param decimalPlace
* @return
*/
public static float round(float d, int decimalPlace) {
BigDecimal bd = new BigDecimal(Float.toString(d));
bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
return bd.floatValue();
}
You need to decide if you want to round up or down. In my sample code I am rounding up.
Hope it helps.
EDIT
If you want to preserve the number of decimals when they are zero (I guess it is just for displaying to the user) you just have to change the function type from float to BigDecimal, like this:
public static BigDecimal round(float d, int decimalPlace) {
BigDecimal bd = new BigDecimal(Float.toString(d));
bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
return bd;
}
And then call the function this way:
float x = 2.3f;
BigDecimal result;
result=round(x,2);
System.out.println(result);
This will print:
2.30
Let's test 3 methods:
1)
public static double round1(double value, int scale) {
return Math.round(value * Math.pow(10, scale)) / Math.pow(10, scale);
}
2)
public static float round2(float number, int scale) {
int pow = 10;
for (int i = 1; i < scale; i++)
pow *= 10;
float tmp = number * pow;
return ( (float) ( (int) ((tmp - (int) tmp) >= 0.5f ? tmp + 1 : tmp) ) ) / pow;
}
3)
public static float round3(float d, int decimalPlace) {
return BigDecimal.valueOf(d).setScale(decimalPlace, BigDecimal.ROUND_HALF_UP).floatValue();
}
Number is 0.23453f
We'll test 100,000 iterations each method.
Results:
Time 1 - 18 ms
Time 2 - 1 ms
Time 3 - 378 ms
Tested on laptop
Intel i3-3310M CPU 2.4GHz
Well this one works...
double roundOff = Math.round(a * 100.0) / 100.0;
Output is
123.14
Or as @Rufein said
double roundOff = (double) Math.round(a * 100) / 100;
this will do it for you as well.
double d = 2.34568;
DecimalFormat f = new DecimalFormat("##.00");
System.out.println(f.format(d));