I am trying to divide two integer and get a decimal number
I am keep getting 0 result when I divide 10/29
I would like to get 0.34
my code is :
private int totalCount_Games;
private int totalCount_Hints_Statistics;
double avgHints;
avgHints=totalCount_Hints_Statistics/totalCount_Games;
In Java, when you divide two integers, the result is another integer.
In your case, 10/29 will result in 0 as you mentioned. If you want to get the results of these in floating digits, then change the above two integers to float or double.
In that case, the result for the above calculation will be 0.34.
PS: This is really basic. You should do more research in the official java site for documentation on datatypes.
The result of a int division is another int, rounded.
Casting int to double (or float) in the expression part will make the division occurs on doubles instead of int, note that this is different from casting the result from the int division to double.
int i = 5, b = 10;
double result = ((double)i)/((double)b);
result is 0.5
The above code will result in 0.0 as int/int will always be a int which is further type casted into double thus output is 0.0
Use the below code, Using big decimal for Rounding
double totalCount_Games_double = totalCount_Games;
double totalCount_Hints_Statistics_double = totalCount_Hints_Statistics;
double value = totalCount_Hints_Statistics/totalCount_Games_double;
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(2, RoundingMode.HALF_UP);
Related
i'm preforming a calculation (converting gigabytes to bytes) but i'm not get the expected results..i believe the calculations and logic is correct but i think it's the way i'm storing my number. i've played with using int, double, float and can't get the right format. i've set the text using toString, valueOf and String.format.
i'm getting 1.0737418E9 instead of 1073741824
relative code below: Thanks!
double sumB;
double sumG;
// get values
Bytes = (EditText)findViewById(R.id.numBytes);
Gigs = (EditText)findViewById(R.id.numGigs);
//calculations
sumB = Double.parseDouble(Gigs.getText().toString()) * 1073741824; //couldn'f figure out how to use exponents
sumG = Double.parseDouble(Gigs.getText().toString()) * 1;
//set text
//Bytes.setText(Integer.toString(sumB));
// Bytes.setText(Double.toString(sumB)); //didnt work
// Bytes.setText(String.format("%.8s", sumB)); //didnt work
Bytes.setText(String.valueOf(sumB));
Gigs.setText(Double.toString(sumG));
Is double that is gotten from a json with Gson is constant and could be compared to a double (from same orgin) with simple "==","=!".
The double is a price for a item(that is 0 or decimal number X.XX), that is compared to a different price.
Or should I use Double.compare(x,y) or x/3==y/3, or something similar.
I think it is better to use BigDecimal in your case.
Another way is selecting precision and doing next comparison
public static final double PRECISION = 0.001;
if (Math.abs(x-y) < PRECISION) {
//x is equal to y
}
I am trying to convert a decimal into a character.
I have my alphabet which has been converted to a charArray
String alphabet = "abcdefghijklmnopqrstuvwxyz";
alpha = alphabet.toCharArray();
I was using binary numbers so values only from 0 to 255, however when I try to execute this code, it does not work.
private int toChar(int encryptCode){
int base = 26;
int characterSteps = (encryptCode/255)*base;
char character = alpha[characterSteps];
return character;
Lets say I have the decimal 78, 78/255 * 26 would give 7.95 (being int rounds to 8)
It should look up the alpha array and give 'h'. But every character gives 'a' meaning that (encryptCode/255)*base isn't working as intended.
Kevin is right in the comments. Try rearranging your formula like this:
int characterSteps = (encryptCode*base)/255;
change
int characterSteps = (encryptCode/255)*base;
to
int characterSteps = (int) (encryptCode/255.0)*base;
and you are done! 255.0 will cause broadening on encryptCode and base, and then cast to int.
Lets say I have the decimal 78, 78/255 * 26 would give 7.95 (being int rounds to 8)
Casting float/double to int will result in truncation and not round off. 7.95 will be truncated to 7 and not 8. To round the integer, use this:
double x = (encryptCode/255.0)*base;
int characterSteps = (int) Math.round(x);
Happy Coding! :)
I'm doing a math operation beetwen hours, but I've a problem with the float (the java virtual machine approximate is not perfect). So, I decided to convert this operation in BigDecimal...but I've some problems with results..
This is the original code:
public float ConvertTo100(float input)
{
float output = 0.0f;
int hh;
float mm;
hh = (int)input;
mm = input - hh;
output = hh + ((input - hh)/60) * 100;
return output;
}
This is my convertion, but doesn't works:
public float ConvertTo100(float input)
{
BigDecimal inputBD = new BigDecimal(Float.toString(input));
String inpString = String.valueOf(input);
String[] inpsplit = inpString.split("\\.");
BigDecimal hh = new BigDecimal(Float.toString(Integer.parseInt(inpsplit[0])));
BigDecimal output = hh.add((inputBD.subtract(hh).divide(BigDecimal.valueOf(60))).multiply(BigDecimal.valueOf(1000)));
return Float.parseFloat(String.valueOf(output));
}
Where is the problem?? Thank you so much!!! :)
Division by 60 cannot be done exactly in decimal for many inputs. To continue using BigDecimal, you will need to pick a scale and rounding mode for the division. Here's one way of doing it, but read the BigDecimal documentation to see all the options.
BigDecimal output = hh.add((inputBD.subtract(hh).divide(
BigDecimal.valueOf(60), 10, BigDecimal.ROUND_HALF_EVEN))
.multiply(BigDecimal.valueOf(1000)));
However, the main point of using BigDecimal is to get exact representation of decimal fractions. You are not getting that benefit, but are getting the messy code that results from doing arithmetic using method calls instead of infix operators. Unless you need a specific rounding mode, or some finite precision greater than 16.9 significant digits, you would be much better off using double.
i am trying to find the power of a value.But the problem is my exponent is a fractional value.power function does not suppporting any datatype other than int.
BigDecimal fd_returns_at_time_of_replace=(BigDecimal.valueOf(capitalDiff).multiply((BigDecimal.valueOf((long)constant1+.09)).pow(temp)));
here temp is a fractional value.given below is the eror message i am getting.
The method pow(int) in the type BigDecimal is not applicable for the arguments (double)
please anybody help me to do this.
BigDecimal.pow() only takes an int. To see a cool example of writing BigDecimal.pow() that accepts a double, see this question How to do a fractional power on BigDecimal in Java?
Common Sense
Consider you want to raise the number x to the power y
If both are integers:
for(int i=0 ; i<y ; i++)
answer = answer * x;
Problems are only when y is a decimal!
So we first change y to the form of y = n + 1/d
How to do that:
n = floor of y
d = 1 / (y - n) << integer
Now x^y = x^n * x^1/d
x to the power n is simple using the usual method
x to the power 1/d is simply the d th root of x
Note: You can increase the precision of your function by reducing the error factor induced by makind d an integer. How! 1/d can be multiplied by powers of 10.