convert double to percent in java 1.5 = 50% - java

i have this code, variables first, second and third only can obtain in double i need format in percent to write later on other place
import java.text.DecimalFormat;
public class Main {
public static void main(String[] argv) throws Exception {
double first = 0.5;
double second = 1.5;
double third = 2.5;
DecimalFormat df = new DecimalFormat("#%");
System.out.println(df.format(first));
System.out.println(df.format(second));
System.out.println(df.format(third));
}
}
output:
50%
150%
250%
i need obtain this result
-50%
50%
150%
i hope any can help me with this thanks

I looks like you need values relative to 100%, so you just need to subtract 1 (i.e. 100%) from your values:
System.out.println(df.format(first - 1));
System.out.println(df.format(second - 1));
System.out.println(df.format(third - 1));

Related

Can't round decimals to 4 decimal points in java for gravity program

So, I'm trying to get the decimals in a table I have for a gravity program that I created to round after 4 decimals. However, whenever I've tried to round it, it either will print the table without rounding, or it will start to print the table, and then throw an error at me.
I'm sure what I doing wrong, and I've tried to look online for help, but it hasn't helped.
I have a few lines commented out below to show where I have tried to round to 4 decimals places, but hasn't worked.
I'll post my code below to see if you guys could please help me figure out what I'm doing wrong.
public class Gravity
{
public static void main(String[] args)
{
Gravity g = new Gravity();
System.out.printf("%5s%20s%20s\n", "Time", "Distance Earth", "Distance Moon");
// Gravity.format()
// DecimalFormat df = new DecimalFormat("###.####");
// System.out.println(df.format(g));
for (int i = 1; i <= 10; i++)
{
System.out.printf("%5d%20fm%20fm\n", + i,g.distanceFallen(i, 9.8), g.distanceFallen(i, 1.625));
// System.out.format("%.4f", i);
}
}
/* private static Gravity format(String string, int i)
{
//
return i;
}*/
public double distanceFallen (double time, double gAcc)
{
// System.out.format("%.4f");
// System.out.format("%.4f", time);
return (0.5)*gAcc*Math.pow(time, 4);
}
}
EDIT: Also, here's what the table looks like, just to make clear up any potential confusion.
Time Distance Earth Distance Moon
1 4.900000m 0.812500m
2 78.400000m 13.000000m
3 396.900000m 65.812500m
4 1254.400000m 208.000000m
5 3062.500000m 507.812500m
6 6350.400000m 1053.000000m
7 11764.900000m 1950.812500m
8 20070.400000m 3328.000000m
9 32148.900000m 5330.812500m
10 49000.000000m 8125.000000m
Change your formatting to:
"%5d%20.4fm%20.4fm\n"
You'll want to change your header formatting to 24 instead of 20 too.

Trying to convert this formula into an arithmetic expression in Java

I'm trying to take user input in the form of myMonthlyPayment, myAnnualInterestRate, and myPrincipal in order to calculate the number of months needed to pay off debt by using The formula I've attached to this post. What I have in eclipse for the formula right now is:
monthsNeeded = ((Math.log(myMonthlyPayment) - Math.log(myMonthlyPayment)
- ((myAnnualInterestRate / 1200.0) * myPrincipal))
/ ((Math.log(myAnnualInterestRate) / 1200.0) + 1.0));
I should be getting an output of 79 months with the inputs I'm using but instead I'm getting -62. I know the formula is correct, I'm almost positive I've made a mistake somewhere in the translation of it into Java. If someone could point it out that would be greatly appreciated!
So I've fixed it, with a sample input and output.
I didn't put much effort into making this code beautiful but you can see that even separating it into 3 parts using method extraction (although I didn't know how to name them, lacking the domain knowledge) made the code easier to understand.
public class Example {
public static void main(String[] args) {
double myMonthlyPayment = 2000;
double myAnnualInterestRate = 5;
double myPrincipal = 200000;
System.out.println(a(myMonthlyPayment));
System.out.println(b(myPrincipal, myAnnualInterestRate, myMonthlyPayment));
System.out.println(c(myAnnualInterestRate));
double monthsNeeded = (a(myMonthlyPayment) - b(myPrincipal, myAnnualInterestRate, myMonthlyPayment))
/ c(myAnnualInterestRate);
System.out.println(monthsNeeded);
}
private static double c(double myAnnualInterestRate) {
return Math.log((myAnnualInterestRate / 1200.0) + 1);
}
private static double b(double myPrinicipal, double myAnnualInterestRate, double myMonthlyPayment) {
return Math.log(myMonthlyPayment - (myAnnualInterestRate / 1200.0) * myPrinicipal);
}
private static double a(double myMonthlyPayment) {
return Math.log(myMonthlyPayment);
}
}
I think this is what you're looking for:
monthsNeeded = (Math.log(myMonthlyPayment) - Math.log(myMonthlyPayment - myAnnualInterestRate / 1200d * myPrincipal)) / Math.log(myAnnualInterestRate / 1200d + 1);
It seems that, in your solution, you weren't calculating your myAnnualInterestRate/1200*myPrincipal inside your second Math.log(...). You had also left some calculations outside of Math.log(...) in the bottom half of your equation.
If you have an equation that does an operation inside a natural log, when you convert that equation to Java code, the operation needs to still be done, inside the natural log:
ln(someNumber + 10)
would be converted to:
Math.log(someNumber + 10),
NOT:
Math.log(someNumber) + 10
Hope this helps and good luck. :)

Calling other methods to main in java

I am having a little issue with formatting returned methods in the main method. I have created the methods and done the calculation, but my issue is if i am calling the other two methods to the main method correctly. I am also having and issue with formatting each method in columns. Do i need to make the columns in the respected methods? or do i need to create them in the main method?
Write a program that analyzes an object falling for 10 seconds. It should contain main and two additional methods. One of the additional methods should return the distance an object falls in meters when passed the current second as an argument. See the formula needed below. The third method should convert meters to feet. You can look up the conversion factor needed online. The main method should use one loop to call the other methods and generate a table as shown below. The table should be displayed in formatted columns with decimals as shown. I believe i am on
SEC METERS FEET
1 4.9 16.1
2 19.6 64.3
3 44.1 144.7
4 78.4 257.2
5 122.5 401.9
6 176.4 578.7
7 240.1 787.7
8 313.6 1028.9
9 396.9 1302.2
10 490.0 1607.6
My code
package week4.yedkois;
public class project3 {
public static void main(String[] args) {
System.out.printf("SEC" + "\n");
meters();
feet();
for (int time = 1; time <= 10; time++) {
System.out.println(time);
}
}
public static void meters() {
double Meters;
double G = 9.8; // meters = .5(9.8)(seconds) ^2
for (int time = 1; time <= 10; time++) {
Meters = (.5 * 9.8 * Math.pow(time, 2));
System.out.printf("%.1f\n", Meters);
}
return;
}
public static void feet() {
double Feet;
double G = 9.8; // meters = .5(9.8)(seconds) ^2
for (int time = 1; time <= 10; time++) {
Feet = (.5 * 9.8 * Math.pow(time, 2) * 3.28084);
System.out.printf("%.1f\n", Feet);
}
return;
}
}
Here is my solution. I use a Tab ("\t") to achieve the same space between the different values. And then I had to redesign your code a little. I use only one if-loop directly in the main-method and hand the current time-value as a parameter into both methods meters() and feet(). That makes it much easier to get all values of one round in line.
Here are some additional remarks:
Java is not C++, so you don't have to use an empty return statement at the end of a method. It's useless there.
In Java variables and method-names always start with a small letter, _ or $. Only class-names and constants start with a capital letter.
Hope this helps for a start.
public class Project3 {
public static void main(String[] args){
System.out.printf("%3s\t%6s\t%6s\n", "SEC", "METERS", "FEET");
for(int time = 1; time <= 10; time++)
{
System.out.print(time + "\t");
meters(time);
feet(time);
System.out.println();
}
}
public static void meters(int time){
double meters;
double g = 9.8; // meters = .5(9.8)(seconds) ^2
meters = (.5 * 9.8 * Math.pow(time, 2));
// the longer the expected maximum length of a result gets
// the higher your reserved number of digits has
// to be, to gain the wanted right bound effect!
System.out.printf("%6.1f\t", meters);
}
public static void feet(int time){
double feet;
double g = 9.8; // meters = .5(9.8)(seconds) ^2
feet = (.5 * 9.8 * Math.pow(time, 2) * 3.28084);
// the longer the expected maximum length of a result gets
// the higher your reserved number of digits has
// to be, to gain the wanted right bound effect!
System.out.printf("%6.1f", feet);
}
}

Java : Rounding a decimal value to HALF_EVEN

I have been trying to write a java code to Round a value to the below requirement.
If x=63.88 => roundedValue= 64.00;
If x=63.50 => roundedValue= 64.00
If x=63.32 => roundedValue= 63.32
I tried with the different roundingModes like CEILING, DOWN, FLOOR, HALFDOWN.
I also tried Math.round();
But I'm unable to get the expected output.
My input is a string and output is a string.
Please find the code snippet I tried below
BigDecimal value1 = new BigDecimal(input);
value1=value1.setScale(2, RoundingMode.HALF_EVEN);
//float rounded=Math.round(amount);
String finalValue=String.valueOf(value1);
I'm unable to get the desired output. Please let me know how to achieve this?
ps: should i consider using float or BigDecimal??
if(x%1 >= .5)
{ x = Math.round(x) }
else //do nothing
This seems like it would give you the desired output you are looking for. So if you really wanted to you could override or create your own method to call for the rounding
What you want to do with this, is providing your own MathContext to specify the behavior of the rounding you want to perform.
The closest you will get to your current requirements is either: using RoundingMode.HALF_UP or RoundingMode.UNNECESSARY
For that you will have to use BigDecimal anyways, since Double and Float do not expose rounding.
public static void main(String args[]) {
Double d = 63.18;
DecimalFormat df = new DecimalFormat("00.00");
if(d % 1 >= 0.5)
System.out.println(df.format(Math.round(d)));
else
System.out.println(d);
}
As in your post, using BigDecimal is the way to go, if you want to use decimal rounding.
If you want to round up for numbers >= X.5 and avoid rounding for numbers < X.5 then you can use this code:
import java.math.BigDecimal;
import java.math.RoundingMode;
public class Round {
public static void main(String[] args) {
System.out.println(round("63.88"));
System.out.println(round("63.50"));
System.out.println(round("63.32"));
}
private static BigDecimal round(String input) {
BigDecimal value = new BigDecimal(input);
BigDecimal rounded = value.setScale(0, RoundingMode.HALF_UP);
if (rounded.compareTo(value) > 0)
return rounded.setScale(2);
return value;
}
}
The output is:
64.00
64.00
63.32

Math I did the program but need to know a little more

I did the problem but I only found out How to do it for one value of x? Here is the program.
public class PointsOnACircleV1 {
public static void main(String[ ] args)
{
double r = 1;
double x = 0.1;
double equation1= Math.pow(r,2);
double equation2= Math.pow(x,2);
double y = Math.sqrt(equation1-equation2);
System.out.println(y);
}
}
I got the correct answer .99 (......)
I need mine to show multiple values of x. Here is how the output should be. Please help if you can.
Use a for loop.
Related Documentation
The for statement.
In order to get it to print multiple values you first need to fill the "String [] args" but you need to ahve them as double to be able to multiply them with other values. In your case that's the X-values, so lets say over the code you posted
public class PointsOnACircleV1 {
//initialize your array with your values
double [ ] args = { 1.0, 0.9, 0.8,.... and so on until you reach 0.1, 0.0};
//you could fill it other more effective ways but just to show you!
public static void main(double[ ] args)
{
double r = 1;
// no need to fill this as you already done
// it double x = 0.1;
for(Iterator<double> i = args.iterator(); args.hasNext(); )
{
//this is the number you want to multiply with
double numbertomultiply = args.next();
double equation1= Math.pow(r,2);
double equation2= Math.pow(numbertomultiply,2);
double y = Math.sqrt(equation1-equation2);
System.out.println(y);
}
}
Just written from my head havent checked it but just to give you a sample :)
EDIT Use the other answers to initialize your array.

Categories