public static double convertFeetandInchesToCentimeter(String feet, String inches) {
double heightInFeet = 0;
double heightInInches = 0;
try {
if (feet != null && feet.trim().length() != 0) {
heightInFeet = Double.parseDouble(feet);
}
if (inches != null && inches.trim().length() != 0) {
heightInInches = Double.parseDouble(inches);
}
} catch (NumberFormatException nfe) {
}
return (heightInFeet * 30.48) + (heightInInches * 2.54);
}
Above is the function for converting Feet and Inches to Centimeter.Below is the function for converting Centimeter back to Feet and Inches.
public static String convertCentimeterToHeight(double d) {
int feetPart = 0;
int inchesPart = 0;
if (String.valueOf(d) != null && String.valueOf(d).trim().length() != 0) {
feetPart = (int) Math.floor((d / 2.54) / 12);
inchesPart = (int) Math.ceil((d / 2.54) - (feetPart * 12));
}
return String.format("%d' %d''", feetPart, inchesPart);
}
I have a problem when i enter normal values like 5 Feet and 6 Inches, its converting perfectly to centimeter and again it gets converted back to 5 Feet and 6 Inches.
The Problem is when i convert 1 Feet and 1 inches or 2 Feet and 2
inches, its getting converted back to 1 Feet 2 inches and 2 Feet 3
inches.
I ran the following code
public class FeetInches{
public static void main(String[] args){
double d = convertFeetandInchesToCentimeter("1","1");
String back_again = convertCentimeterToHeight(d);
System.out.println(back_again);
}
public static double convertFeetandInchesToCentimeter(String feet, String inches) {
double heightInFeet = 0;
double heightInInches = 0;
try {
if (feet != null && feet.trim().length() != 0) {
heightInFeet = Double.parseDouble(feet);
}
if (inches != null && inches.trim().length() != 0) {
heightInInches = Double.parseDouble(inches);
}
} catch (NumberFormatException nfe) {
}
return (heightInFeet * 30.48) + (heightInInches * 2.54);
}
public static String convertCentimeterToHeight(double d) {
int feetPart = 0;
int inchesPart = 0;
if (String.valueOf(d) != null && String.valueOf(d).trim().length() != 0) {
feetPart = (int) Math.floor((d / 2.54) / 12);
System.out.println((d / 2.54) - (feetPart * 12));
inchesPart = (int) Math.ceil((d / 2.54) - (feetPart * 12));
}
return String.format("%d' %d''", feetPart, inchesPart);
}
}
And got
1.0000000000000018
1' 2''
By using the ceiling function you are rounding up to 2 when you really want to be rounding down to 1.
I believe:
inchesPart = (int) Math.ceil((d / 2.54) - (feetPart * 12));
Should be:
inchesPart = (int) Math.floor((d / 2.54) - (feetPart * 12));
The problem is because of the way java handles floating point numbers.
inchesPart = (int) Math.ceil(Math.round((d / 2.54) - (feetPart * 12)));
or
inchesPart = (int) Math.floor((d / 2.54) - (feetPart * 12));
In case of input 2,2 the original value of inchesPart is 2.0000000000000036 -> ceil ->3
The main issue with you're code is that you're not using the same rounding function for each part :
int feetPart = (int) Math.floor((d / 2.54) / 12);
^^^^^
int inchesPart = (int) Math.ceil((d / 2.54) - (feetPart * 12));
^^^^
You should also do the rounding before the decomposition in order to obtain consistent results :
int feetPart = ((int) Math.round(d / 2.54)) / 12;
int inchesPart = ((int) Math.round((d / 2.54)) - (feetPart * 12);
Which could be factorized to:
int inches = (int) Math.round(d / 2.54);
int feetPart = inches / 12;
int inchesPart = inches - (feetPart * 12);
Or since ( inches - ( ( inches / 12 ) * 12) ) == ( inches % 12 ):
int inches = (int) Math.round(d / 2.54);
feetPart = inches / 12;
inchesPart = inches % 12;
You can interchange Math.round with Math.floor or Math.ceil depending on the result you expect.
I know this is old may be useful for someone else (Kotlin version)
fun feetToCentimeter(feetval: String): String {
var heightInFeet = 0.0
var heightInInches = 0.0
var feet = ""
var inches = ""
if (!TextUtils.isEmpty(feetval)) {
if (feetval.contains("'")) {
feet = feetval.substring(0, feetval.indexOf("'"))
}
if (feetval.contains("\"")) {
inches = feetval.substring(feetval.indexOf("'") + 1, feetval.indexOf("\""))
}
}
try {
if (feet.trim { it <= ' ' }.isNotEmpty()) {
heightInFeet = feet.toDouble()
}
if (inches.trim { it <= ' ' }.isNotEmpty()) {
heightInInches = inches.toDouble()
}
} catch (nfe: NumberFormatException) {
}
return (((heightInFeet * 12.0) + heightInInches) * 2.54).toString()
}
fun centimeterToFeet(centemeter: String?): String {
var feetPart = 0
var inchesPart = 0
if (!TextUtils.isEmpty(centemeter)) {
val dCentimeter = java.lang.Double.valueOf(centemeter!!)
feetPart = Math.floor((dCentimeter / 2.54) / 12).toInt()
println(dCentimeter / 2.54 - feetPart * 12)
inchesPart = Math.floor((dCentimeter / 2.54) - (feetPart * 12)).toInt()
}
return String.format("%d' %d\"", feetPart, inchesPart)
}
Related
I had a working code, which used ParallelCamera when zooming, and it worked perfectly.
Here's it:
double s = camera.getScaleX();
double d = e.getDeltaY();
double f = 1.2;
double x = e.getSceneX();
double y = e.getSceneY();
if (d < 0) {
f = 2;
} else if (d > 0) {
f = 0.5;
}
double ps = s;
/*if (s * f > 1) {
camera.setScaleX(1);
camera.setScaleY(1);
camera.setScaleZ(1);
camera.setTranslateX(0);
camera.setTranslateY(0);
return;
}
camera.setScaleX(s * f);
camera.setScaleY(s * f);
camera.setScaleZ(s * f);
s = camera.getScaleX();
double dx = ps * (1 - f) * x;
double dy = ps * (1 - f) * y;
System.out.println(dx);
double width = primaryScene.getWidth();
double height = primaryScene.getHeight();
if (camera.getTranslateX() + s * width + dx > width) {
camera.setTranslateX(width - s * width);
} else if ((camera.getTranslateX() + dx < 0)) {
camera.setTranslateX(0);
} else {
camera.setTranslateX(camera.getTranslateX() + dx);
}
if (camera.getTranslateY() + s * height + dy > height) {
camera.setTranslateY(height - s * height);
} else if ((camera.getTranslateY() + dy < 0)) {
camera.setTranslateY(0);
} else {
camera.setTranslateY(camera.getTranslateY() + dy);
}
But I wanted to not have the quality loss, so I rewrote it.
Now it scales the Pane, and not the camera.
I followed exactly the same code, only this time the scaling is reversed.
And when I calculate the position, I first set an offset tomimic my original code.
(The camera's pivot point is at 0,0, but the Pane's is in the center)
The quality loss is gone, but it doesnt really work:
double s = mapLayer.getScaleX();
double d = e.getDeltaY();
double f = 1.2;
double width = primaryScene.getWidth();
double height = primaryScene.getHeight();
double x = (e.getSceneX());
double y = (e.getSceneY());
if (d > 0) {
f = 2;
} else{
f = 0.5;
}
double ps = s;
mapLayer.setScaleX(s * f);
mapLayer.setScaleY(s * f);
mapLayer.setScaleZ(s * f);
s = mapLayer.getScaleX();
double dx = ps * (1 - f) * x;
double dy = ps * (1 - f) * y;
mapLayer.setTranslateX(mapLayer.getTranslateX()+ps*(width/2)*(f-1));
mapLayer.setTranslateY(mapLayer.getTranslateY()+ps*(height/2)*(f-1));
mapLayer.setTranslateX(mapLayer.getTranslateX()+dx);
mapLayer.setTranslateY(mapLayer.getTranslateY()+dy);
I found this, and it worked like a charm:
JavaFX 8 - Zooming Relative to Mouse Pointer. The solution now is this (without clamping):
double delta = 1.2;
double scale = mapLayer.getScaleX(); // currently we only use Y, same value is used for X
double oldScale = scale;
if (e.getDeltaY() < 0) {
scale /= delta;
} else {
scale *= delta;
}
double f = (scale / oldScale) - 1;
double dx = (e.getSceneX()
- (mapLayer.getBoundsInParent().getWidth() / 2
+ mapLayer.getBoundsInParent().getMinX()));
double dy = (e.getSceneY()
- (mapLayer.getBoundsInParent().getHeight() / 2
+ mapLayer.getBoundsInParent().getMinY()));
mapLayer.setScaleX(scale);
mapLayer.setScaleY(scale);
mapLayer.setScaleZ(scale);
mapLayer.setTranslateX(mapLayer.getTranslateX() - f * dx);
mapLayer.setTranslateY(mapLayer.getTranslateY() - f * dy);
Currently I zoom in and out using a ParallelCamera, but everything stays on the scene resolution when I zoom in.
But if I scale the group itself, not the camera then it is extremely slow, sometimes freezes the entire application.
What is the correct way of zooming?
My group contains about 4,000 Polygons, nothing else.
Here's the current code:
double s = camera.getScaleX();
double d = e.getDeltaY();
double f = 1.2;
double x = e.getSceneX();
double y = e.getSceneY();
if (d < 0) {
f = 1.1;
} else if (d > 0) {
f = 0.9;
}
double ps = s;
if (s * f > 1) {
camera.setScaleX(1);
camera.setScaleY(1);
camera.setScaleZ(1);
camera.setTranslateX(0);
camera.setTranslateY(0);
return;
}
camera.setScaleX(s * f);
camera.setScaleY(s * f);
camera.setScaleZ(s * f);
s = camera.getScaleX();
double dx = ps * (1 - f) * x;
double dy = ps * (1 - f) * y;
double width = primaryScene.getWidth();
double height = primaryScene.getHeight();
if (camera.getTranslateX() + s * width + dx > width) {
camera.setTranslateX(width - s * width);
} else if ((camera.getTranslateX() + dx < 0)) {
camera.setTranslateX(0);
} else {
camera.setTranslateX(camera.getTranslateX() + dx);
}
if (camera.getTranslateY() + s * height + dy > height) {
camera.setTranslateY(height - s * height);
} else if ((camera.getTranslateY() + dy < 0)) {
camera.setTranslateY(0);
} else {
camera.setTranslateY(camera.getTranslateY() + dy);
}
My code calculates the average height of a child once they become adult using their parents heights
I am getting height for the child being 7 feet+ when the mother and father are between 4-6 feet
Hmale_child = ((Hmother * 13 / 12) + Hfather ) / 2
Hfemale_child = ((Hfather * 12 / 13) + Hmother) / 2
if (gender.equalsIgnoreCase("Male")) {
childHeightInch = (((monHeightInch + (monHeightFeet * 12)) * 13 / 12) + (dadHeightInch + (dadHeightFeet * 12)) / 2);
childHeightFeet = childHeightInch / 12;
childHeightInch = childHeightInch % 12;
} else if (gender.equalsIgnoreCase("Female")) {
childHeightInch = (((dadHeightInch + (dadHeightFeet * 12)) * 12 / 13) + (monHeightInch + (monHeightFeet * 12)) / 2);
childHeightFeet = childHeightInch / 12;
childHeightInch = childHeightInch % 12;
Add some Brackets,it is only dividing dad height by 2
childHeightInch = (((((monHeightInch + (monHeightFeet * 12)) * 13) / 12) + (dadHeightInch + (dadHeightFeet * 12))) / 2);
childHeightInch = (((((dadHeightInch + (dadHeightFeet * 12)) * 12) / 13) + (monHeightInch + (monHeightFeet * 12))) / 2);
If you try to do all the calculations on one line, then no matter how you do it, you'll end up with far too many parentheses. The result is that it will be difficult to read the code accurately, and you'll make mistakes. As indeed you have done.
Don't write any parentheses that you don't mathematically need. And consider calculating the total heights of Mum and Dad separately first, as shown below.
int dadsHeightTotal = dadsHeightInches + dadsHeightFeet * 12;
int mumsHeightTotal = mumsHeightInches + mumsHeightFeet * 12;
if (gender.equalsIgnoreCase("male")) {
childsHeightTotal = ( mumsHeightTotal * 13 / 12 + dadsHeightTotal ) / 2;
} else {
childsHeightTotal = ( dadsHeightTotal * 12 / 13 + mumsHeightTotal ) / 2;
}
childsHeightFeet = childsHeightTotal / 12;
childsHeightInches = childsHeightTotal % 12;
I have this code, and the input in the textfield is 50, so its output should be 25,
but there is a error, because no output
button_1.addActionListener(new ActionListener() {
String tfg = textField.getText().toString();
String tfc = textField.getText();
public void actionPerformed(ActionEvent e) {
if(textField.getText().toString().equals("0") ||
textField.getText().toString().equals("") ||
textField.getText().toString().equals(" ") ||
textField.getText().toString().matches("[a-zA-Z]+")) {
JOptionPane.showMessageDialog(Cow.this,
"Tree Amount Should Be 1 or More",
"Invalid Tree Amount",
JOptionPane.ERROR_MESSAGE);
}
if(!tfg.equals("0")) {
try {
int tfgg = Integer.parseInt(tfc);
int trcs = tfgg * 1;
double trcp = 1 / 2;
int trcc = (int) trcp * trcs;
System.out.println(new Integer(trcc).toString());
} catch (NumberFormatException se) {
}
}
}
});
I try to convert from my php to that code, here is my php code :
$doorSeed = $mount * 1;
$doorPrice = 1 / 2;
$doorConvert = $doorPrice * $doorSeed;
echo "Treasure Chest Seed = ".$doorSeed." Price : ".$doorConvert." World Lock <br>";
int trcc = (int) trcp * trcs;
Try this instead
int trcc = (int) (trcp * trcs);
You need to cast the complete expression [(trcp * trcs)] instead of just the first variable trcp to int. The other variable in the expression trcs is of double type, so the result of the expression become double. That is why you cast the complete expression. So your end result will be int
int tfgg = Integer.parseInt(tfc.trim()); // tfc = 4; tfgg = 4;
int trcs = tfgg * 1; // trcs = 4 * 1 = 5;
double trcp = 1.0 / 2; // trcp = 0.5;
int trcc = (int) (trcp * trcs); // trcc = (0.5 * 5) = (2.5) = 2;
System.out.println(trcc); // 2;
double trcp = 1.0 / 2;
Here make at least one value as double so the expression will be evaluated as double as suggested by #tunaki.
Also you don't need this statement in your code int trcs = tfgg * 1;.
Use like this:
String tfg ;
String tfc ;
public void actionPerformed(ActionEvent e) {
tfg = textField.getText();
tfc = textField.getText();
if(textField.getText().equals("0") || textField.getText().equals("") || textField.getText().equals(" ") || textField.getText().matches("[a-zA-Z]+")) {
JOptionPane.showMessageDialog(Cow.this,
"Tree Amount Should Be 1 or More",
"Invalid Tree Amount",
JOptionPane.ERROR_MESSAGE);
}
if(!tfg.equals("0")) {
try {
int tfgg = Integer.parseInt(tfc.trim()); // tfc = 4;
int trcs = tfgg; // trcs = 4 * 1 = 5
double trcp = 1.0 / 2; // trcp = 0.5
int trcc = (int) (trcp * trcs); // trcc = (0.5 * 5) = (2.5) = 2
System.out.println("HI: "+trcc);
} catch (NumberFormatException ignored) {
}
Your problem is at this line:
double trcp = 1 / 2;
This does not result in trcp = 0.5 but trcp = 0.0, because you are dividing integer values (and so are using integer division).
You should use the following code:
double trcp = 1.0 / 2;
to force division using doubles.
Other comments:
new Integer(trcc).toString() should be replaced with String.valueOf(trcc).
Avoid using empty catch statements: at minimum, log the exception
What's the point of this line: int trcs = tfgg * 1;?
Basically you have this (int)0.5 = 0.
In your case, trcp = 0.5 and is double, so when it's cast to integer the result is 0.
And when you do (int)a + b, the true operation with parenthesis for priority here is: ((int)a)+(b), so what you have to is the following:
int trcc = (int) (trcp * trcs);
instead of the following:
int trcc = (int) trcp * trcs;
double totalInches = d * 0.3937;
double feetPart = totalInches / 12;
int inchesPart = (int) Math.ceil(totalInches - (feetPart * 12));
return (feetPart) + "' " + inchesPart + "''";
I am getting a value 6.9999999 ' 0". I am returning a string, is it the reason why the decimals values in feet is not getting rounded off.
I tried without casting too. double inchesPart = Math.ceil(totalInches - (feetPart * 12));, but still i get the same result.
Surely you need:
int feetPart = (int)Math.floor(totalInches / 12);
or just:
int feetPart = (int)(totalInches / 12);
To get the two parts you can use
int totalInches = (int) (d * 0.3937);
int feetPart = totalInches / 12;
int feetInchPart = totalInches % 12;