private void findLatLongDistance(double prelat,double prelon,double lat,double lon) {
// TODO Auto-generated method stub
try{
double prelatval=prelat;
double prelongval=prelon;
double curlat=lat;
double curlon=lon;
Log.w("inside finalatlon...........................","the daya");
if(prelatval>0.0 && prelongval>0.0 && lat>0.0 && lat>0.0 && gpsdataElements.Speed>0.0){
float distance2 = getDistance(prelatval,prelongval,curlat,curlon);
odometer_sum= (distance2/1000 );
// for maximum value after decimal
//df.setMaximumFractionDigits(0);
//rounding the km digits after decimal
Math.round(distance2);
//for minimum distance after decimal
df.setMinimumFractionDigits(2);
df.format(odometer_sum);
gpsdataElements.Distance = gpsdataElements.Distance + odometer_sum;
}
}catch(Exception e){
e.printStackTrace();
}
}
here is my code. I want distance(i.e km)value digits after decimal is two digits. Example i want km value in 1.54km not like 1.4568923km. How to get like this.I tried a lot for that but i din't got any possible solution. Any one know please help me.
In more simple ways you can do this by using this :
double roundOff = Math.round(yourValue * 100.0) / 100.0;
Otherwise you can also do this as
String.format("%.2f", d)
DecimalFormat formatter = new DecimalFormat(".##");
String s = formatter.format(value);
use this code to format text in two digits after decimal.
double i2=i/60000;
tv.setText(new DecimalFormat("##.##").format(i2));
double roundof = Math.round(a * 100.0) / 100.0;
output:roundoff(eg:12).oo
12.00
Something like this :
String.format("%.2f", distance);
Make function like this pass the value as per you requirment and return value as per your requirment first function will take float value and after decimal it put 3 value as per argument if you want to change decimal value then you can change 3,2,4 etc... Second function will take String value and round it in 3 decimal point:
public static BigDecimal round(float d) {
BigDecimal bd = new BigDecimal(Float.toString(d));
bd = bd.setScale(3, BigDecimal.ROUND_HALF_UP);
return bd;
}
public static BigDecimal round(String d) {
BigDecimal bd = new BigDecimal(Float.parseFloat(d));
bd = bd.setScale(3, BigDecimal.ROUND_HALF_UP);
return bd;
}
Related
Should be simple I know but I cant find an answer anywhere. I'm trying to round up to two decimal places, so if my answer is 164.9835 I'd like the answer to be displayed as 164.99. But what I have so far is rounding it to 164.98 for some reason.
Any help much appreciated.
double number1 = Double.parseDouble(num1.getText().toString());
double number2 = Double.parseDouble(num2.getText().toString());
double number3 = Double.parseDouble(num3.getText().toString());
double number4 = Double.parseDouble(num4.getText().toString());
double sum = (((number1 * number2)/1000)*0.5)*(number3 - number4);
total.setText (String.format("£%s", new java.text.DecimalFormat("##.##").format(sum)));
If you want to round up you can use this method
cantDecimal = 2;
number = 164.9835
public static double aroundUp(double number, int canDecimal) {
int cifras = (int) Math.pow(10, canDecimal);
return Math.ceil(number * cifras) / cifras;
}
return = 164.99
Extra: Ceil Method in Math.
The method ceil gives the smallest integer that is greater than or equal to the argument.
How to sum of arrays of floating point numbers..as the results from distance that the array float come from results Location.distanceBetween(latStart, longStart, latB, longB, results); method in Google maps api ,,i'm trying with this, but at some point it decrease, it should always incrementing. what am i missing?
EDIT:
public float total(ArrayList<LatLng> listPoints){
if(listPoints.size()==2){
listPoints.clear();
}
listPoints.add(latLng);
float[] results = new float[2];
float sum = 0.0f;
for (int z = 0; z < listPoints.size(); z++) {
if(listPoints.size() == 1){
latStart = listPoints.get(z).latitude;
longStart = listPoints.get(z).longitude;
Toast.makeText(this, "listPoint 1", Toast.LENGTH_SHORT).show();
}else if(listPoints.size() == 2){
latB = listPoints.get(z).latitude;
longB = listPoints.get(z).longitude;
Toast.makeText(this, "listPoint 2", Toast.LENGTH_SHORT).show();
}
Location.distanceBetween(latStart, longStart, latB, longB, results);
sum+=results[0];
}
tvJarakTotal.setText(sum + "");
return sum;
}
EDIT:
the distance came from onLocationChanged() method as the user location is moving,
EDIT:
I Finally find the solution myself, by creating some trick using sharedPreference. but the accepted answer is correct to the question.
It is not entirely clear what you think you were doing in your code above. First of all, the Location#distanceBetween API takes in a pair of latitude longitude values, i.e. two geographic points, and then returns the distance between them in results[0] (q.v. the documentation).
Next, it isn't clear what the starting and ending points are intended to be. I answered below under the assumption that the listPoints are one set of points (either starting or ending), and the vales latB and longB are a fixed set of starting/ending points. Without this assumption, an answer really can't be given here.
public float total(ArrayList<LatLng> listPoints) {
float[] results;
float sum = 0.0f;
for (int z=0; z < listPoints.size(); z++) {
double latStart = listPoints.get(z).latitude;
double longStart = listPoints.get(z).longitude;
Location.distanceBetween(latStart, longStart, latB, longB, results);
sum += results[0];
tvJarak.setText(sum + "");
}
return sum;
}
Please take a look your code carefully.
float[] results = new float[listPoints.size()];
results variable is declared as a local variable but it's not initialized with any values yet.
In this case it has random value.
That's why sum gives random minus value.
float[] results = new float[listPoints.size()];
In this case your result array stored size of the listPoints array. After that you are trying to iterate result array by using listPointers array size.
Assume your :- float [] results = new [2];
Now you try to add up same value again and again inside you for a loop. Please make sure your scenario is correct or not.
You should store api date someware inside your code. like this:
lat = listPoints.get(z).latitude;
long = listPoints.get(z).longitude;
if you want you can do it like this,
public float total(ArrayList<LatLng> listPoints) {
float[] results;
float sum = 0.0f;
float lat = 0.0f, long = 0.0f;
for (int z=0; z < listPoints.size(); z++) {
lat = listPoints.get(z).latitude;
long = listPoints.get(z).longitude;
Location.distanceBetween(lat, long, latB, longB, results);
sum+=results[z];
tvJarak.setText(sum + "");
}
return sum;
}
I want to calculate the circumference of a circle and put the results in a TextView.
public void hesapla1(View v){
double pi = 3.14;
double yari = R.id.e1;
double alan = pi * Math.pow(yari,2);
sonuc1.setText((int) alan);
}
You can only set a String value in a TextView:
sonuc1.setText(String.valueOf(alan));
Why I can not see the double value in a textView?
textView = (TextView) findViewById(R.id.textView);
double x = 5/2;
textView.setText(String.valueOf(x)); // I see this result as 2.0 and not as the 2.5
In Android TextView, you can use in that way,
Use Double.toString:
result = number1/number2
String stringdouble= Double.toString(result);
textview1.setText(stringdouble));
or you can use the NumberFormat:
Double result = number1/number2;
NumberFormat nm = NumberFormat.getNumberInstance();
textview1.setText(nm.format(result));
To force for 3 units precision:
private static DecimalFormat REAL_FORMATTER = new DecimalFormat("0.###");
textview1.setText(REAL_FORMATTER.format(result));
You are seeing 2.0 because 5/2 is integer division. Change the line to double x = 5.0/2; or similar.
See this question for more.
This should work double x = ((double) 5) /2.
I will Format a Floating Point Number in android with 2 Digits after the point.
I search for a Example but i din´t find one.
Can some one help me.
float f = 2.3455f;
String test = String.format("%.02f", f);
You can also use DecimalFormat
float f = 102.236569f;
DecimalFormat decimalFormat = new DecimalFormat("#.##");
float twoDigitsF = Float.valueOf(decimalFormat.format(f)); // output is 102.24
//double twoDigitsF = Double.valueOf(decimalFormat.format(f)); also format double value
In answer above it always will return 2 deciaml
examples with String.format("%.02f", f)
2.5555 -> 2.55
2.5 -> 2.50
but do you need to get 2.50 for 2.5?
for avoid to get excessive 0, you can use this solution
val format = DecimalFormat("0.##")
return format.format(floatNum)