I have problem in dealing with scientific notation. In order to simplifeid problem, I used constants instead of variables, and the statement as follows :
double temp = 211.751 * 110 * 500 * 0.9971;
Log.e("Temp : ", Double.toString(temp));
the result is : 1.1612530715499999E7
what I want is : 11,612,530.72
My question is how to convert the result into normal expression and formatted to 2 decimal places.
Double.parseDouble("9.78313E+2");
gives you
978.313
and then
NumberFormat formatter = new DecimalFormat("#0.00");
System.out.println(formatter.format(number));
try it out
// try this
double temp = 211.751 * 110 * 500 * 0.9971;
DecimalFormat format = new DecimalFormat("#00,000,000.00");
Log.e("Temp : ",format.format(temp));
Related
I'm working on an app and facing an issue. I've tried a number of solutions but nothing solved my problem.
I need to round off two digits after decimal point.
For Example.
9.225 should be rounded off to 9.23
Thank you.
For Kotlin use "%.2f".format(number), for Java use String.format("%.2f", number)
Result:
You can use String.format("%.2f", d), this will rounded automatically. d is your value.
OR
You can use this
double d = 1.234567;
DecimalFormat df = new DecimalFormat("#.##");
Log.d(df.format(d));
You can get as a float value as well like below.
float value = Float.valueOf(df.format(d)); // Output will be 1.24
I would have gone with a probable over the top solution however this is what i came up with.
It uses regex to split the string value of the number passed and then rounds up/down depending on the leading digit after the decimal place. It will return a Double in the instance but you can change that if you like. It does throw IllegalArgumentException, but thats taste dependant.
/**
* #param value the value that is being transformed
* #param decimalPlace the decimal place you want to return to
* #return transformed value to the decimal place
* #throws IllegalArgumentException
*/
Double roundNumber(#NonNull Double value, #NonNull Integer decimalPlace) throws IllegalArgumentException {
String valueString = value.toString();
if(valueString.length()> decimalPlace+1){
throw new IllegalArgumentException(String.format("The string value of %s is not long enough to have %dplaces", valueString, decimalPlace));
}
Pattern pattern = Pattern.compile("(\\d)('.')(\\d)");
Matcher matcher = pattern.matcher(valueString);
if (matcher.groupCount() != 4) { //0 = entire pattern, so 4 should be the total ?
throw new IllegalArgumentException(String.format("The string value of %s does not contain three groups.", valueString));
}
String decimal = matcher.group(3);
int place = decimal.charAt(decimalPlace);
int afterDecimalPlace = decimal.charAt(decimalPlace + 1);
String newDecimal = decimal.substring(0, decimalPlace - 1);
newDecimal += afterDecimalPlace > 5 ? (place + 1) : place;
return Double.parseDouble(matcher.group(1) + "." + newDecimal);
}
Example :
value = 0.0
I want to covert it to
Ans :value = 00.0
example
value = 12.0
Ans: 12.0
example
value = 1.0
Ans: 01.0
Right now for float values i am using String.format( " %.2f" ,variable_value)
is there any way like this to convert values
Thanks
you can use string.format method like below:
String value= "_%02d" + "_%s";
it converts numbers like :
String.format("%02d", 1); // => "01"
you can see forrmatter doc for other modifiers
I have a method to calculate Difference between two Latitude:
public static double distanceKM(LatLng latLng1, LatLng latLng2) {
int EARTH_RADIUS_KM = 6371;
double lat1Rad = Math.toRadians(latLng1.latitude);
double lat2Rad = Math.toRadians(latLng2.latitude);
double deltaLonRad = Math.toRadians(latLng2.longitude - latLng1.longitude);
double dist_travelled = Math
.acos(Math.sin(lat1Rad) * Math.sin(lat2Rad) + Math.cos(lat1Rad)
* Math.cos(lat2Rad) * Math.cos(deltaLonRad))
* EARTH_RADIUS_KM;
dist_travelled = Double.parseDouble(new DecimalFormat("##.######")
.format(dist_travelled));
return dist_travelled;
}
Sometimes, this method throw Exception (I say sometimes, when I test in defference device):
java.lang.NumberFormatException: Invalid double: "0,179927"
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:269)
Can someone help me in this case? Thanks
The value of the double depends on the language of the device. For example, for devices in french the number 0.179927 becomes 0,179927 which will always throw a NumberFormatException when parsing it to double because of the comma.
You need to change the separator from a comma to a point.
You can change the separator either by setting a locale or using the DecimalFormatSymbols.
If you want the grouping separator to be a point, you can use a european locale:
NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
DecimalFormat df = (DecimalFormat)nf;
Alternatively you can use the DecimalFormatSymbols class to change the symbols that appear in the formatted numbers produced by the format method. These symbols include the decimal separator, the grouping separator, the minus sign, and the percent sign, among others:
DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(currentLocale);
otherSymbols.setDecimalSeparator(',');
otherSymbols.setGroupingSeparator('.');
DecimalFormat df = new DecimalFormat(formatString, otherSymbols);
Hi friends I want to get two values after decimal,but in my I am getting 0 after the decimal. this is my code below.
if (!ed.getText().toString().equals("")) {
if (rb1.isChecked()) {
int input = Integer.valueOf(ed.getText().toString());
double out = input / 24;
out = (double)Math.round(out * 100)/100;
Intent m = new Intent(Page.this,MActivity.class);
m.putDoubleExtra("res", out);
startActivity(m);
I referred these sites also: Android : How to get just two digit after the point in decimal value ? dont want to trunc the value
Java : how do I get the part after the decimal point?
and I tried the code below also but it getting 0 after decimal.
DecimalFormat newFormat = new DecimalFormat("#.##");
double twoDecimal = Double.valueOf(newFormat.format(d))
Please help me new to coding. Thanks in advance.
Since input and 24 are integers, when you divide it, it still produces int. Only then it is converted to double.
One way to solve this: double out = input / 24.0;
DecimalFormat newFormat = new DecimalFormat("#.00");
double twoDecimal = Double.valueOf(newFormat.format(d))
In your code i found is
out = (double)Math.round(out * 100.0)/100.0;
In my app i am using this to calculate a number:
double result = ((a * a) + (b * b) - (2 * a * b));
If a for example a = 2 and b = 3.9, then the result should be 3.61. However, when I display "result" i get the number 3.610000000000001.
What is wrong here? How can I correct this error?
You can format your output value by using the following construct:
String result;
twoDForm = new DecimalFormat("#0.00");
result = twoDForm.format(value);
After this, in result you will have your value in X.XX format