I use DecimalFormat in Anroid Studio. I had a double value and I want to decimalformat. But, when I give double value in decimalformat; they said: "Found String Value". But, it's a double value. I try convert to string but it doesn't work. What can I do?
double lat = (latPoint * 180.0 / Math.PI);
DecimalFormat DF = new DecimalFormat("#.########");
Double latlast = DF.format(lat);
DF.format(lat) is returning a String, you cannot format a Double to anything you want.
A double cannot hold all trailing zeroes for example:
System.out.println((double) 1.0000); //Will print 1.0
To print it to anything you want you must print it as a String:
System.out.println("1.0000"); //Will print 1.0000
Which is why .format returns a String rather than a double.
Change the final line to this:
String latlast = DF.format(lat);
EDIT: Try this instead if you want to truncate a decimal place to 6 digits.
double lat = (latPoint * 180.0 / Math.PI);
double latFloor = Math.floor(lat * 1000000) / 1000000; //6 decimal places
DecimalFormat.format(double val) returns String, not Double.
double lat = (latPoint * 180.0 / Math.PI);
DecimalFormat DF = new DecimalFormat("#.########");
String latlast = DF.format(lat);
If you want to format double into double with some requirements then you can convert it again because
double lat = (latPoint * 180.0 / Math.PI);
DecimalFormat format = new DecimalFormat("#.####");
String format1 = format.format(ad);
this code returns formatted double into string form.
So you have to convert it again into double again with this
double number = Double.parseDouble(format1);
Related
The formula to calculate the area of a circumference is defined as x =
π . R2. Considering to this problem that π = 3.14159:
Calculate the area using the formula given in the problem description.
Input The input contains a value of floating point (double precision),
that is the variable R.
And for an input of 2, I should be getting x=12.5664 rounded by one number.
I tried using this simple code, but I couldn't remember what to do with the "cannot convert from double to float" error. It's been half a year since I coded.
package TEST;
import java.util.Scanner;
public class TEST {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// let A be radius
float A = scanner.nextFloat();
float A_2 = A * A;
// let B be Pi
double B = 3.14159;
// let x be circumference
float x = A_2 * B;
System.out.printf("x= %.4f" + x);
}}
The cause of the compilation error is the following assignment:
float x = A_2 * B;
where B is of type, double and therefore the result of the product will of type, double which can not be accommodated into a variable of type, float. Remember: double requires 8 bytes of space whereas a float variable can accommodate only 4 bytes.
After correcting this compilation error, you will encounter a runtime error because you have used a plus sign (+) instead of a comma (,) inside the printf statement.
Apart from this,
Always follow Java naming conventions e.g. A should be a and A_2 should be a2 following the conventions.
You can use Math.PI instead of using your own value for PI.
The following code incorporates these changes:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// let a be radius
float a = scanner.nextFloat();
float a2 = a * a;
// let b be PI
double b = Math.PI;
// let x be circumference
double x = a2 * b;
System.out.printf("x= %.4f", x);
}
}
A sample run:
2
x= 12.5664
You could try:
double A_2Converted = (double) A_2;
and use that.
source: Convert float to double without losing precision
I'm having difficulty even beginning to solve this problem. All examples that I have found are either too simple or way too complex to digest.
I want to to find the value S given a series of inputs. The function is univariate but non-linear. S will always be between -3 .. 3.
I would like to use the Apache Commons library, as I have had prior experience in other sections of that code.
For each time I want to solve my problem, I know the following information:
double R =250.0;
double om1 = 5.0;
double om2 = 15.0;
double th21 = 29.07965;
double th22 = 29.69008;
double D_obs = th21 - th22;
The actual values will change between solutions, but they are fixed for any one particular solution.
The value I want to find is:
double S = 0.0;
such that
double d1 = delta(R,om1,th21,S);
double d2 = delta(R,om2,th22,S);
double D_calc = d1 - d2;
have values to make
double minme = Math.abs(D_obs - D_calc);
a minimum, or alternately, solve
double minme = D_obs - D_calc;
where minme=0.
The function delta is defined as
public static double delta(double R, double om, double th2, double s)
{
if(Math.abs(s) <= 0.0001) //is the displacement == 0?
{
return 0.0;
}
else
{
return Math.toDegrees((-1*Cos(th2)*s-R*Sin(om)+Sqrt(-1*Math.pow(Cos(th2),2)*Math.pow(s,2)+2*Cos(th2)*Sin(om)*R*s-Math.pow(Cos(om),2)*Math.pow(R,2)+Math.pow(R,2)+2*Math.pow(s,2)))/(Sin(th2)*s));
}
}
where, for example, Cosis defined elsewhere as Math.cos(Math.toRadians(val))
Where/what can I read/do to get a start on this problem?
I found an answer I could work with: Newton-Raphson method using the Math.Commons library
The key code is
public static void main(String args[])
{
//setup all variables
final double R =(new Double(args[0])).doubleValue(); //=250.0;
final double om1 =(new Double(args[1])).doubleValue(); //= 5.0;
final double om2 =(new Double(args[2])).doubleValue(); //= 15.0;
final double th21=(new Double(args[3])).doubleValue(); //= 29.07965;
final double th22=(new Double(args[4])).doubleValue(); //= 29.69008;
final double D_obs = th21 - th22;
BisectionSolver solver = new BisectionSolver();
UnivariateFunction f = new UnivariateFunction()
{
public double value(double s) {
return ((delta(R,om1,th21,s)-delta(R,om2,th22,s)) - (D_obs));
}
};
System.out.printf("The speciment offset is %.3f mm.\n", solver.solve(1000, f, -3, 3));
}
So i have this code
BigDecimal bd = new BigDecimal(i);
bd = bd.round(new MathContext(6));
double meters = bd.doubleValue();
double km = bd.doubleValue()*0.001;
double cm = bd.doubleValue()*100;
double mm = bd.doubleValue()*1000;
double miles = bd.doubleValue()*0.000621371192;
double inches = bd.doubleValue()*39.3700787;
double feet = bd.doubleValue()*3.2808399;
double yards = bd.doubleValue()*1.0936133;
double points = bd.doubleValue()*2834.64567;
So i found an example on here that told me to do it like so
BigDecimal bd = new BigDecimal(i);
bd = bd.round(new MathContext(6));
double meters = bd.doubleValue();
and thats what i tried, and it round the meters number however the inches, feet, yards and so on numbers dont get rounded. am i doing this wrong? what would be the right way to go about this?
EDIT: okay so i found something that has worked in case anyone else ever has a problem with it
instead of using :
double points = bd.doubleValue()*2834.64567;
i did this:
BigDecimal points = new BigDecimal(i*2834.64567);
points = points.round(new MathContext(6));
You can use BigDecimal;
For example:
double d = 15.3343243;
BigDecimal bd = new BigDecimal(d);
bd = bd.setScale(2, BigDecimal.ROUND_CEILING);
bd = new BigDecimal(0.001 * bd.doubleValue());
d = bd.doubleValue();
System.out.println(d);
or without BigDecimal objects:
System.out.println((double)((int) (d * 1000)) / 1000);
I'm having a hard time wrapping my head around some Trigonometry. I am trying to deduce a destination latitude and longitude from a start lat and log and distance and bearing.
Fortunately, I found an amazing site which describes exactly the function I need:
http://www.movable-type.co.uk/scripts/latlong.html " Destination point given distance and bearing from start point "
I tried it in my java program but it is not working for me. I deployed it as the website said. Here is my code:
double dist = 150/6371;
double brng = Math.toRadians(90);
double lat1 = Math.toRadians(26.88288045572338);
double lon1 = Math.toRadians(75.78369140625);
double lat2 = Math.asin( Math.sin(lat1)*Math.cos(dist) + Math.cos(lat1)*Math.sin(dist)*Math.cos(brng) );
double a = Math.atan2(Math.sin(brng)*Math.sin(dist)*Math.cos(lat1), Math.cos(dist)-Math.sin(lat1)*Math.sin(lat2));
System.out.println("a = " + a);
double lon2 = lon1 + a;
lon2 = (lon2+ 3*Math.PI) % (2*Math.PI) - Math.PI;
System.out.println("Latitude = "+Math.toDegrees(lat2)+"\nLongitude = "+Math.toDegrees(lon2));
But it shows the output is:
a = 0.0
Latitude = 26.882880455723377
Longitude = 75.78369140625
I am not getting where i am doing the mistake. Please anybody can help me to find out the problem.
Thanx in Advance. :-)
Your problem is on your first line.
Try
double dist = 150.0 / 6371.0;
The reason is that 150/6371 gets calculated as 0, because it performs integer division (rather than floating point division). This is true even though the result is being stored in a double. You can force floating point division by making one of the two numbers a floating point literal.
if anyone needs a function that calculates point coordinates from other point moved by some distance, below is the working code. For me, it's just moving a point by some distance.
import static java.lang.Math.*;
void movePoint(double latitude, double longitude, double distanceInMetres, double bearing) {
double brngRad = toRadians(bearing);
double latRad = toRadians(latitude);
double lonRad = toRadians(longitude);
int earthRadiusInMetres = 6371000;
double distFrac = distanceInMetres / earthRadiusInMetres;
double latitudeResult = asin(sin(latRad) * cos(distFrac) + cos(latRad) * sin(distFrac) * cos(brngRad));
double a = atan2(sin(brngRad) * sin(distFrac) * cos(latRad), cos(distFrac) - sin(latRad) * sin(latitudeResult));
double longitudeResult = (lonRad + a + 3 * PI) % (2 * PI) - PI;
System.out.println("latitude: " + toDegrees(latitudeResult) + ", longitude: " + toDegrees(longitudeResult));
}
latitude, longitude - entry point coordinates
distanceInMetres - distance that you want to move the point by
bearing - an angle, direction towards which you want to move the point. 0 is towards the North, 90 - East, 180 - South, 270 - West. And all between, i.e. 45 is North East.
earthRadiusInMetres - Earth radius in metres.
You can change the radius to 6371 if you want to have the input in kilometres or to miles if you want to have input in miles.
I want to calculate the difference between two double values
for example : lat1=12.2345673 and lat2=12.2345672 . here i want result as 0.0000001. this much exactly i am not getting while calculate double res=Double.compare(lat1,lat2) in eclipse. it showing 0.0.
Please specify the exact formula to overcome this
Can u try the following code ,
double lat1=12.2345673;
double lat2=12.2345672;
double dif=lat1-lat2;
DecimalFormat df = new DecimalFormat("###.#######");
System.out.println("Diff Val : "+df.format(dif));
Output :
Diff Val : 0.0000001
You can use following method for calculating distance between two lat-long points.
/**
*
* #param lat1 Latitude of the First Location
* #param lng1 Logitude of the First Location
* #param lat2 Latitude of the Second Location
* #param lng2 Longitude of the Second Location
* #return distance between two lat-lon in float format
*/
public static float distFrom (float lat1, float lng1, float lat2, float lng2 )
{
double earthRadius = 3958.75;
double dLat = Math.toRadians(lat2-lat1);
double dLng = Math.toRadians(lng2-lng1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLng/2) * Math.sin(dLng/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = earthRadius * c;
int meterConversion = 1609;
return new Float(dist * meterConversion).floatValue();
}
Multiply both numbers by 10 million, then subtract, then divide by same would be one solution.
Turn it into an int.
When I work with Lat/Long values I usually work with 1E6 accuracy and store as an int.
I learned this pattern from the GeoPoint class in Google's Android Map library.
Looks like you need more than 1E6 though, so you might want to use higher accuracy and use long instead.
Use BigDecimal.