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);
Related
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);
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));
}
Dear more advanced programmers,
I have to programm a Pythagoras tree (see: https://en.wikipedia.org/wiki/Pythagoras_tree_(fractal) ) and I want to take a step forward, but I just can't.
I want to create the 3rd point of the triangle, but after 2 days of searching in books, internet, my head, etc., I just did not find a solution. Well, I have a solution, but it wont work for the recursive step and I don't know how to create the 3rd and 4th point of the following squares.
I want my programm to draw the step 1 of the tree.
Here's my code for the calculation so far:
public static void main(String[] args) {
double ax;
double ay;
double bx;
double by;
double cx;
double cy;
double dx;
double dy;
ax = 0.25;
ay = 0.75;
bx = 0.25;
by = 0.25;
cx = 0.75;
cy = 0.25;
dx = 0.75;
dy = 0.75;
StdDraw.line(ax,ay,bx,by); // ax etc. are doubles
StdDraw.line(bx,by,cx,cy);
StdDraw.line(cx,cy,dx,dy);
StdDraw.line(dx,dy,ax,ay);
double alpha;
double beta;
double random = 0.0;
while(random <= 0.3 || random >= 0.6){ //random angular between 30&60
random = Math.random();
}
random = random*100; //to make it an angular
alpha = random; //set alpha to this angular
beta = 90-alpha;
double sinusBeta= Math.toRadians(beta);
double sinusAlpha= Math.toRadians(alpha);
sinusBeta = Math.sin(sinusBeta);
sinusAlpha = Math.sin(sinusAlpha);
double side_b = 0.5*sinusBeta;
double side_a = 0.5*sinusAlpha;
double hypothenuse = Math.sqrt((side_b*side_b)+(side_a*side_a));
double p = (side_a*side_a)/hypothenuse; //to get h of the triangle
double q= (side_b*side_b)/hypothenuse;
double h = Math.sqrt(p*q);
double triangleTop_x = ax+q;
double triangleTop_y = ay+h;
StdDraw.line(ax, ay, triangleTop_x , triangleTop_y);
StdDraw.line(dx, dy, triangleTop_x , triangleTop_y);
}
I know that this is not the right way to get the last triangle point cause it has the condition that the hypothenuse is exactly in the direction of the x-axis. Which is not true for step two anymore, so a recursive method would not behave as it should. I wrote all the code by my own, I know it is not the best one, but it would be awesome to get an idea of how I can build the triangle and the following squares not direction-dependent.
Thank you so much in advance for any ideas that may help!
Hey guys i have problem making percentege of double number.
double pom = cumulativeProbability(GetZ(baby));
Log.d("Pom","" + pom);
double pom1 = round(cumulativeProbability(GetZ(baby)),2);
Log.d("Pom1", "" + pom1);
double pom2 = pom1 * 100;
Log.d("Pom2: ", "" + pom2);
Here are logs:
06-05 02:30:04.574 13208-13208/com.bojan.readfromcsv D/Pom﹕ 0.5821510290598115
06-05 02:30:04.584 13208-13208/com.bojan.readfromcsv D/Pom1﹕ 0.58
06-05 02:30:04.584 13208-13208/com.bojan.readfromcsv D/Pom2:﹕ 57.99999999999999
I want to round this given pom value into two decimals and that i get with this round function:
public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
}
For final result pom2 I want to have 58 %.... Tnx
Hopefully this will help:
public class t {
public static void main(String args[]) {
double pom = 0.5821510290598115;
System.out.println("pom = "+pom);
double pom1 = Math.round(pom*100)/100.0;
System.out.println("pom1 = "+pom1);
System.out.println("pom2 = "+Math.round(pom1*100));
}
}
Which produces:
$ java t
pom = 0.5821510290598115
pom1 = 0.58
pom2 = 58
If you want to preserve some more digits in pom2, say 2, do this:
System.out.println("pom2 = "+Math.round(pom1*100*100)/100.0);
Try:
double pom2 = round(pom1 * 100, 0);
EDIT:
int pom2 = (int) round(pom1 * 100, 0);
I assume you aren't having to write your own round function, so why not use:
pom2 = (int) Math.round(pom1 * 100);
I am trying to create a visual grid of this http://www.ibm.com/developerworks/library/j-coordconvert/ -Military Grid Reference System. I have the latitude/longitude to UTM and also to MGRS...which ar
17 T 330649 4689666
17TLG3064989666
But when going from MGRS to latitude I get the following:
[D#18e3f02a
public class CoordinateConversion {
public static void main(String args[]) {
CoordinateConversion test = new CoordinateConversion();
CoordinateConversion test2 = new CoordinateConversion();
test.latLon2UTM(35.58, 82.56);
System.out.println(test.latLon2UTM(42.340837, -83.055821));
System.out.println();
test2.latLon2UTM(35.58, 82.56);
System.out.println(test2.latLon2MGRUTM(42.340837, -83.055821));
CoordinateConversion test3 = new CoordinateConversion();
test3.latLon2UTM(35.58, 82.56);
//System.out.print(test3.mgrutm2LatLong(42.340837, -83.055821));
//System.out.println(test3.mgrutm2LatLong("02CNR0634657742"));
MGRUTM2LatLon mg = new MGRUTM2LatLon();
//mg.convertMGRUTMToLatLong("02CNR0634657742");
String MGRUTM = "17TLG3064989666";
System.out.println(mg.convertMGRUTMToLatLong(MGRUTM));
//for loop to be developed
}
public double[] utm2LatLon(String UTM) {
UTM2LatLon c = new UTM2LatLon();
return c.convertUTMToLatLong(UTM);
}
public double[] mgrutm2LatLon(String MGRUTM) {
MGRUTM2LatLon c = new MGRUTM2LatLon();
return c.convertMGRUTMToLatLong(MGRUTM);
}
}
and from this class:
public double[] convertMGRUTMToLatLong(String mgrutm) {
double[] latlon = {0.0, 0.0};
// 02CNR0634657742
int zone = Integer.parseInt(mgrutm.substring(0, 2));
String latZone = mgrutm.substring(2, 3);
String digraph1 = mgrutm.substring(3, 4);
String digraph2 = mgrutm.substring(4, 5);
easting = Double.parseDouble(mgrutm.substring(5, 10));
northing = Double.parseDouble(mgrutm.substring(10, 15));
LatZones lz = new LatZones();
double latZoneDegree = lz.getLatZoneDegree(latZone);
double a1 = latZoneDegree * 40000000 / 360.0;
double a2 = 2000000 * Math.floor(a1 / 2000000.0);
Digraphs digraphs = new Digraphs();
double digraph2Index = digraphs.getDigraph2Index(digraph2);
double startindexEquator = 1;
if ((1 + zone % 2) == 1) {
startindexEquator = 6;
}
double a3 = a2 + (digraph2Index - startindexEquator) * 100000;
if (a3 <= 0) {
a3 = 10000000 + a3;
}
northing = a3 + northing;
zoneCM = -183 + 6 * zone;
double digraph1Index = digraphs.getDigraph1Index(digraph1);
int a5 = 1 + zone % 3;
double[] a6 = {16, 0, 8};
double a7 = 100000 * (digraph1Index - a6[a5 - 1]);
easting = easting + a7;
setVariables();
double latitude = 0;
latitude = 180 * (phi1 - fact1 * (fact2 + fact3 + fact4)) / Math.PI;
if (latZoneDegree < 0) {
latitude = 90 - latitude;
}
double d = _a2 * 180 / Math.PI;
double longitude = zoneCM - d;
if (getHemisphere(latZone).equals("S")) {
latitude = -latitude;
}
latlon[0] = latitude;
latlon[1] = longitude;
return latlon;
}
I am trying not to get into a large library where I will have to learn things that may be time consuming.
So I am trying to loop so I go east (easting) and north (northing) and cannot get past the point where I have one point - latitude/longitude.
Hope I have asked my question clearly without stating too much.
Any help will be appreciated.
Thanks,
-Terry
Your result from convertMGRUTMToLatLong() is an array of doubles, and by default, arrays are converted to String in a rather unreadable format in Java. That's where the [D#18e3f02a comes from. Try System.out.println(Arrays.toString(mg.convertMGRUTMToLatLong(MGRUTM))); and you'll get a more readable output.
In the method convertMGRUTMToLatLong(String s) you are returning an array latlon (i.e an object).
It returns its hashcode which is probably u dont want.
You want to print the array values. So in your main method you replace below line;
System.out.println(mg.convertMGRUTMToLatLong(MGRUTM));
with
double[] a = mg.convertMGRUTMToLatLong(MGRUTM) ;
System.out.println(a[0]+" " + a[1] );
Hope that helps!