Precision in String - java

I have the following code:
String price="9000.89";
double basePrice =10000;
String dis= String.valueOf(((basePrice - Double
.parseDouble(price)) * 100 / basePrice));
System.out.println(dis);
It is giving me the output as "9.991100000000007", but i need it in 2 decimal points. Any idea how to do that?
Expected output : 9.99

String price="9000.89";
double basePrice =10000;
String dis = String.format("%.2f",((basePrice - Double.parseDouble(price)) * 100 / basePrice));
System.out.println(dis);

I would recommend
double dis = ((basePrice - Double.parseDouble(price)) * 100 / basePrice);
System.out.format("%1.2f", dis);

You can round using:
Math.round(number * 100.0) / 100.0;

Related

Format Python Floating Point Numbers like Java does

My program consists of a Java and a Python component and I want Python to format floating point numbers exactly in the same way as Java does by default.
Java:
float number1 = (float) Math.PI;
float number2 = (float) Math.PI / 1000f;
float number3 = (float) Math.PI / 10000f;
float number4 = (float) Math.PI / 100000f;
float number5 = (float) (int)(Math.PI * 1000000f) / 1000000f / 100000f;
System.out.println(number1); // prints 3.1415927
System.out.println(number2); // prints 0.0031415927
System.out.println(number3); // prints 3.1415926E-4
System.out.println(number4); // prints 3.1415926E-5
System.out.println(number5); // prints 3.141592E-5
How to get the same output in Python?
First attempts in Python:
import math
number1 = float(math.pi)
number2 = float(math.pi / 1000)
number3 = float(math.pi / 10000)
number4 = float(math.pi / 100000)
number5 = float(int(math.pi * 1000000) / 1000000 / 100000)
print(number1) # prints 3.141592653589793
print(number2) # prints 0.0031415926535897933
print(number3) # prints 0.0003141592653589793
print(number4) # prints 3.141592653589793e-05
print(number5) # prints 3.141592e-05
print("{:8e}".format(number1)) # prints 3.141593e+00
print("{:8e}".format(number2)) # prints 3.141593e-03
print("{:8e}".format(number3)) # prints 3.141593e-04
print("{:8e}".format(number4)) # prints 3.141593e-05
print("{:8e}".format(number5)) # prints 3.141592e-05
Your formatting syntax is incorrect. Do this instead
print("{:.15f}".format(math.pi))
or the old fashioned way
print("%.15f" % (math.pi))

Variable might not have been initialized? Error

I am working on an app and it doesn't really works out at the moment. The app calculates how much calories you burned with walking the stairs. But when I want to round up the amount of calories it says Variable might not have been initialized. So I made the double kcal = 0 . But when I tested my app it just gave the number 0.0 as the calories I burned that day. Do you guys know how to resolve this problem?
Here is my code where I Declare the doubles:
double trap;
double kcal = 0;
double endkcal = Math.round( kcal * 100.0 ) / 100.0;
double keer;
double gewicht;
double nul = 0.13;
String eten = "Je kan nu Niks eten";
And here is the calculating code:
trap = Double.parseDouble(editText_trap.getText().toString());
gewicht = Double.parseDouble(editText_gewicht.getText().toString());
keer = gewicht * nul / 4;
kcal = keer * trap;
textView_kcal.setText(String.valueOf(endkcal) + "kcal");
You should use endkcal = Math.round( kcal * 100.0 ) / 100.0; after you calculate kcal to have correct calculation. Now it is normal to get 0.0 after calculation because kcal is 0.
Apparently your endkcal variable is only initialized (to 0.0) but never updated.
Initialize all of your double variables when you have declared them like this:
double trap=0.0;
double kcal =0.0;
double endkcal = Math.round( kcal * 100.0 ) / 100.0;
double keer=0.0;
double gewicht=0.0;
double nul = 0.13;

Haversin formula for distance less than 5 meter

I am trying to use the haversin formula to prevent updating the mysql table. I am implementing an app based on crowdsorucing where the data mac, route, lat, long is being recorded by the devices of the passangers im bus and send to the server.
My database table has mac address as UNIQUE KEY. So to avoid that other passanger in the same bus to store their data in the table I would filter these requests by using the Haversin formula but I tried it with tow point which are roughly 20 meter close to each other but I am gettig number of 4803.800129810092
//calculate the distance between the request's sender and all other request in the ArrayList.
private double haversineDistance(LatLong x, LatLong y) {
final int R = 6371; // Radious of the earth
double xLat = x.getLatitude();
double xLon = x.getLongitude();
double yLat = y.getLongitude();
double yLon = y.getLongitude();
;
double latDistance = toRad(yLat - xLat);
double lonDistance = toRad(yLon - xLon);
double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
+ Math.cos(toRad(xLat)) * Math.cos(toRad(yLat))
* Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double distance = R * c;
System.out.println("The distance between two lat and long is:" + distance);
return distance;
}
Haversine is overkill for this. Pythagoras is adequate.
Below is javascript function to return distances in metres. I am sure you can convert to Java'
function Pyth(lat1,lat2,lng1,lng2){
x = toRad(lng2-lng1) ;
y = toRad(lat2-lat1);
R = 6371000; // gives d in metres
d = sqrt(x*x + y*y) * R;
return d;
}

How to put this equation in java code?

This is what I did but no matter what I keep getting infinity:
public double calcr(){
double cot = 1 / Math.tan(0);
return .5 * sideLength * cot * (Math.PI / numSides);
}
Main:
RegularPolygon poly = new RegularPolygon(4, 10);
System.out.println(poly.calcr());
Output:
Inifinity
What am I doing wrong?
The problem is that you do
double cot = 1 / Math.tan(0);
which will make cot be Infinity.
You'd want:
double cot = 1 / Math.tan(Math.PI / numSides);
return .5 * sideLength * cot;
Or, in a single line:
return .5 * sideLength / Math.tan(Math.PI / numSides);
tan(0) is 0, so this line
double cot = 1 / Math.tan(0);
sets cot to Infinity. The computation below it will then also evaluate to Infinity, as you saw.
Since it looks like you're trying to evaluate cot(pi/n), you'd need 1 / Math.tan(Math.PI / n) instead of using cot * (Math.PI / numSides) with an incorrect value for cot.

Java sphere volume calculation

I have a Java class and im stumped on this issue. We have to make a volume calculator. You input the diamater of a sphere, and the program spits out the volume. It works fine with whole numbers but whenever I throw a decimal at it, it crashes. Im assuming it has to do with the precision of the variable
double sphereDiam;
double sphereRadius;
double sphereVolume;
System.out.println("Enter the diamater of a sphere:");
sphereDiam = keyboard.nextInt();
sphereRadius = (sphereDiam / 2.0);
sphereVolume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( sphereRadius, 3 );
System.out.println("The volume is: " + sphereVolume);
So, like I said if i put in a whole number, it works fine. But I put in 25.4 and it crashes on me.
This is because keyboard.nextInt() is expecting an int, not a float or double. You can change it to:
float sphereDiam;
double sphereRadius;
double sphereVolume;
System.out.println("Enter the diamater of a sphere:");
sphereDiam = keyboard.nextFloat();
sphereRadius = (sphereDiam / 2.0);
sphereVolume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( sphereRadius, 3 );
System.out.println("The volume is: " + sphereVolume);
nextFloat() and nextDouble() will pickup int types as well and automatically convert them to the desired type.
double sphereDiam;
double sphereRadius;
double sphereVolume;
System.out.println("Enter the diameter of a sphere:");
sphereDiam = keyboard.nextDouble();
sphereRadius = (sphereDiam / 2.0);
sphereVolume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( sphereRadius, 3 );
System.out.println("");
System.out.println("The volume is: " + sphereVolume);

Categories