I'am trying to multiply A double Variable as time. For example I have 2 hours and 30 minutes. SO the procedure that I have to follow is to multiply 2 with 15 hours for example and then Divide 30 minutes with 60 that is the minutes of the hour and then multiply it with 15. Here is my code:
Double water_time = Double.valueOf(jTextField1.getText());
String[] seperated = String.valueOf(water_time).split("\\.");
int[] intseperated = new int[2];
intseperated[0] = Integer.parseInt(seperated[0]);
intseperated[1] = Integer.parseInt(seperated[1]);
int price1 = intseperated[0] * propertiesFile.multiply_water;
double price2 = (intseperated[1] * propertiesFile.multiply_water) / 60.0D;
double price = (price1 + price2);
My Problem is when iam type 0.10 I get (0.25) that is the correct value but when I type 0.11 I get (2.75) that is wrong. And I cant Understand Why.
I appreciate your time.
Thank you in advance
The issue is in the way you're declaring Double water_time.
0.10 becomes Double 0.1 so then you're actually divide 1 by 4, and get mathematically INCORRECT 0.25
0.11 becomes Double 0.11 as you may expect, and you then divide 11 by 4, and mathematically CORRECT 2.75
To fix - split String first , then turn values as Doubles and calculate accordingly.
Working code:
String[] seperated = jTextField1.getText().split("\\.");
int[] intseperated = new int[2];
intseperated[0] = Integer.parseInt(seperated[0]);
intseperated[1] = Integer.parseInt(seperated[1]);
int price1 = intseperated[0] * propertiesFile.multiply_water;
double price2 = (intseperated[1] * propertiesFile.multiply_water) / 60.0D;
double price = (price1 + price2);
As far as I understood, you want to divide the decimal value by 60 and then multiply of by 15. But what you have done is first multiplied by 15, then divided the whole answer by 60.
double price2 = (intseperated[1] / 60.0D) * propertiesFile.multiply_water;
Related
I was wondering on how I could take 10% away from a double value in Java? I've tried researching percentages in Java, but it's a bit confusing.
You can multiply 0.1 instead of using percents.
For example:
/*Your starting value*/
int x = 100
/* Your percent you want to take away*/
int y = 0.1 /*0.1 is the same as 10%*/
int z = x*y
/* A takes your starting amount and takes away 10% */
int a = x-z
/* a is your answer */
A more simple way of doing this would be to multiply by 0.9 (it multiplys by 90%).
/*Your starting number*/
int x = 100
/*What you want to take away*/
int y = 0.9
int z = x*y
This does take less code and eqations.
(COMPLETE newbie here).
I've got an extra sheet with simple exercises to complete. One of the exercises says to write a program where user can input a random amount of money and the program tells the user how many euros, 50 cents, 20 cents, 10 cents, 5 cents, 2 cents and 1 cents are needed to make up the amount entered.
I try to use the modulus to get the amount of cents needed,
My question is how do I get my program to calculate the number of CENTS needed?
Everytime I use division operator to get the number of 50 cents needed on the remainder of the amount (ex. 0.60) it rounds the double up giving me 2 as output.
Scanner keyboard = new Scanner(System.in);
System.out.print("Input money recieved: ");
double recieve = keyboard.nextDouble();
double cents = recieve * 100;
int centsTotal = (int)cents;
System.out.print("Cents in total " + centsTotal);
int notes = centsTotal / 100;
System.out.print("\nEuros: " + notes);
double remain = recieve % notes;
System.out.printf("\nRemain: %.2f",remain);
double remain2 = remain / 0.5;
System.out.println("\nTest: ",remain2);
My output is:
Input money recieved: 45,78
Cents in total 4578
Euros: 45
Remain: 0,78
Test: 1.5600000000000023
You can cheat by using the Math.round() function like so:
double roundOff = Math.round(valueToRound * 100.0) / 100.0;
This ensures that you keep the value as a double, which allows you to do further operations on it if necessary.
One way is maybe a little brutal, but memory efficient (create no new object or variables). Simply cast your double to int to round down.
public class RoundingTest {
public static void main(String[] args) {
double num = 1.56;
System.out.println("Test: " + (int)num);
}
}
Outputs:
Test: 1
This also allows you to keep your original double untouched for further calculation if needed.
I am making a lottery type game and using Math.random() for the numbers. I want it to always print out what number you got in relation to 0 - 100 (so if Math.random outputted 0.03454 and the number to win was below 0.05, it would set the text of a label to 5). How would you make it round to just a 0.00 number?
Here is some of the code if you want to see what I mean.
public void lotterymath()
{
double x = Math.random();
System.out.println(x);
if (x <= 0.02)
output.setText("you win " + x);
else
output.setText( "you lost " + x);
}
I also have a button below that calls lotterymath() by the way :)
Edit: misread original post:
You will want to multiply by 100, and then cast to an int to truncate it, or Math.round it instead:
System.out.println(Math.round(x*100)); // rounds up or down
or
System.out.println((int) (x*100));
Original:
Use String.format(String, Object...):
System.out.println(String.format("%.2f", x));
The %.2f is a format string.
Have you tried
Math.round(x)
Checkout this link for the documentation: http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#round(double)
EDIT:
I might not have fully understanded your question, but I think if you use
Math.round(Math.random*100)
You'll get a number between 0 and 100.
I prefer to use BigDecimal when dealing with floating point numbers
BigDecimal myRounded = new BigDeicmal(Math.random()).setScale(2, BigDecimal.ROUND_HALF_UP);
Since Math.random() returns a double between 0.0 to 1.0, you can just multiply the result with 100. So 0.0 * 100 = 0, 1.0 * 100 = 100, and everything in between will always be between 0 and 100.
Use Math.round() to get a full integer number. So if the random number is 0.03454, multiplied by 100 = 3.454. Round it to get 3.
correct:
int var = (int)Math.round(Math.random()*100)
INCORRECT:
int var = Math.round(Math.random()*100)
you need to downcast to integer before assign to integer variable in order to don't get an error like this:
error: incompatible types: possible lossy conversion from long to int
int var = Math.round( Math.random() * 3);
^
When you create the variable multiply it by 100 like so:
double a = Math.random()*100;
then when you have to print it put an (int) before the variable just like down here:
System.out.print((int)a);
I have a value like this:
421.18834
And I have to round it mathematical correctly with a mask which can look like this:
0.05
0.04
0.1
For example, if the mask is 0.04, i have to get the value 421.20, because .18 is nearer at .20 than .16.
All functions that I found using Google didn't work.
Can you please help me?
double initial = 421.18834;
double range = 0.04;
int factor = Math.round(initial / range); // 10530 - will round to correct value
double result = factor * range; // 421.20
You don't need a special function. You multiply your original number by (1/mask), you round it to a decimal and you divide again by the same factor.
Example with 0.05
factor = 1/0.05 = 20
421.18834 * 20 = 8423.7668
int( 8423.7668 ) = 8424
8424.0 / 20.0 = 421.20
Example with 0.01
factor = 1/0.1 = 10
421.18834 * 10 = 4211.8834
int( 4211.8834 ) = 4212
4212.0 / 10.0 = 421.20
Contrary to all the answers you will probably get here about multiplying and dividing, you can't do this accurately because floating point doesn't have decimal places. To need to convert to a decimal radix and then round. BigDecimal does that.
Both fredley and Matteo make the assumption that the rounding factor is itself a factor of 100. For factors like 0.06 or 0.07, this is an incorrect assumption.
Here's my Java routine:
public double rounded(double number, double factor) {
long integer = (long) number;
double fraction = number - integer;
double multiple = (fraction / factor);
multiple = Math.round(multiple);
return factor * multiple + integer;
}
Hey guys, I am trying to round to 3 decimal places.
I used the following code.
this.hours = Round(hours + (mins / 60), 3);
But it's not working.
Where have I gone wrong?
Thanks
You can use this function:
public static double Round(double number, int decimals)
{
double mod = Math.pow(10.0, decimals);
return Math.round(number * mod ) / mod;
}
First thing is that all your variables are int so the result of your division is also an int, so nothing to Round.
Then take a look to: How to round a number to n decimal places in Java
If mins is an integer, then mins / 60 will result in an integer division, which always results in 0.
Try changing from 60 to 60.0 to make sure that the division is treated as a floating point division.
Example:
int hours = 5;
int mins = 7;
// This gives 5.0
System.out.println(Math.round(1000 * (hours + (mins / 60 ))) / 1000.0);
// While this gives the correct value 5.117. (.0 was added)
System.out.println(Math.round(1000 * (hours + (mins / 60.0))) / 1000.0);
if mins is an integer you have to divide through 60.0 to get a floating number which you can round
try using like follow
this.hours = Round(hours + (((double)mins) / 60), 3);
You can't. Doubles don't have decimal places, because they are binary, not decimal. You can convert it to something that does have decimal places, i.e. a base-ten number, e.g. BigDecimal, and adjust the precision, or you can format it for output with the facilities of java.text, e.g. DecimalFormat, or the appropriate System.out.printf() string.