Java Double formatting String output [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Not suitable for this site
Improve this question
I'm having some problem formatting decimals of a double. The only error is:
String output = DecimalFormat = (((P * t) / 360) * 0.025);
DecimalFormat df = new DecimalFormat(" ###,###.00");
String output = DecimalFormat = (((P * t) / 360) * 0.025);

DecimalFormat df = new DecimalFormat("###,###.00");
String output = df.format(((30 * 45) / 360) * 0.025);
In your second line, you have DecimalFormat = which should be replaced with df.format The DecimalFormat class doesn't let you format decimals, it's the df object that you created which can format decimals by calling the format method.

Related

How to calculate gravitational force? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am getting the wrong output for the code below. G = 6.674 x 10^-11. I am supposed to get 3.559466666666664e+22 but I am getting 8.0088E44. Can someone please explain what I have wrong on my code? I will appreciate it. The inputs are 2e30 6e24 1.5e11
import stdlib.StdOut;
public class GraviForce {
public static void main(String[] args) {
double m1 = Double.parseDouble(args [0]);
double m2 = Double.parseDouble(args [1]);
double r = Double.parseDouble(args [2]);
double G = 6.674e-11;
double f = G * (m1 * m2) / r * r;
StdOut.println(f);
A simple operator associativity mistake - missing parens:
double f = G * (m1 * m2) / (r * r);

Put point in string value (android java) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have BMI calculator app that takes some input and puts it in "EditText". Here is what I am trying to do:
If the input is 170, it will become 1.70.
If the input is 1.70, it will not change.
This is the code I have:
String weight = editText.getText().toString();
Cant you convert the string to an integer and take modulus 100 to the cms and divide by 100 to get in meters?
You can convert the String weight to int like this
int wgt = Integer.parseInt(weight);
Then separating meters and centimetres.
int mtrs = wgt / 100;
int cms = wgt % 100;
Then combining both
String result = mtrs + "." + cms;
try something like this
float value = Float.parse("170");
editText.setText(String.Format(Locale.ENGLISH,"%,02f", value/100f))
I found this way works: I take the number that the user put, and then "170/100" + "0" gives me 1.70

DecimalFormat Spaghetti [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
First time using the DecimalFormat classes...
I need to store 4 decimals precision for my value.
Let me show you what I have done so far.
public class Paper
{
public String name, color, type,finish,grain;
public double width,height,gsm, lbs,ppi;
public DecimalFormat df;
public Paper()
{
String pattern = "#0.0000";
df = new DecimalFormat(pattern);
}
public void setWidthAndHeight(String width, String height)
{
//I've censured what I tried so far.
///I need to ensure that the passed width has 4 decimals and that it is stored that way.
this.width = //ENTER answer here! :)
}
Thank you for your time, and patience.
You can use BigDecimal to manage your decimals.
BigDecimal n = new BigDecimal("100.12345");
n = n.setScale(4, RoundingMode.HALF_UP);
n = n.stripTrailingZeros();
System.out.println(n.toPlainString());
Using scale will round your number to 4 decimal precision. Remove any zeros using stripTrailingZeros and use toPlainString to get your number later for printing.
http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html

Converting String to Double in Java [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am having an issue with an error I am getting when I compile my program. The only issue is that it needs a double and I have a String. I tried to cast it as a double (both variables), but that didn't seem to work. Any help would be appreciated. Here are the lines of code the issue is coming from. Both qualityPoints and sumOfHours are integers and GPA is a double. The error is on the third line.
DecimalFormat df = new DecimalFormat("0.00");
//GPA = (qualityPoints / sumOfHours);
GPA = df.format (((double)qualityPoints) /(double)(sumOfHours));
Error Code:
GPACalculator.java:123: error: incompatible types
GPA = df.format (((double)qualityPoints) /(double)(sumOfHours));
^
required: double
found: String
1 error
Thanks for any help.
GPA = Double.parseDouble(df.format (((double)qualityPoints) /(double)(sumOfHours)));
int qualityPoints = 0;
int sumOfHours = 0;
double GPA = 0;
DecimalFormat df = new DecimalFormat("0.00");
GPA = Double.parseDouble(df.format (((double)qualityPoints) /(double)(sumOfHours)));
No compilation error for this.
Considering that you have a valid double value in qualityPoints string then you can try to use Double.valueOf(String s) method :
GPA = df.format ((Double.valueOf(qualityPoints)) /(double)(sumOfHours));
Does this work?
GPA = df.format ( Double.valueOf(qualityPoints) / Double.valueOf(sumOfHours) );

find sin-1 of user input for ladder hight [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm trying to find the sin of a number that the user inputs say for eg 1.5. I've done it on the calculator and it works but the code is not working.
Here is the code:
package msd1;
import java.util.Scanner;
public class Part3
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number");
double Height = scanner.nextDouble();
double Angle = Height/2;
double asine = Math.asin(Angle);
System.out.println("Arcsine of " + Angle + " = " + asine);
}
}
Your variable names make no sense. A "height" is a length, and dividing a length by 2 doesn't give you an "angle". Furthermore, you don't pass an "angle" to asin, you pass it a number from -1 to +1 and it returns an angle.
In your case, you'd want to want to take the height of the ladder and divide it by its length to give you your sin (between -1 and +1), then take the asin of that value.
Odds are you also want to take the angle returned by asin in radians and convert to degrees.
You might have code like this:
double lengthOfLadder = 2.0;
double height = scanner.nextDouble();
double sine = height / lengthOfLadder;
double angleInRadians = Math.asin(sine);
double angleInDegrees = angleInRadians / Math.PI * 180;

Categories

Resources