Operation from float to BigDecimal in Java - android

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.

Related

getting result of decimal number

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);

unexpected results (double vs float vs int)

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));

compare doubles that are from a parsed JSON using GSON

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
}

how to use a double value in power function

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.

Floating Point with 2 Digits after Point

I will Format a Floating Point Number in android with 2 Digits after the point.
I search for a Example but i dinĀ“t find one.
Can some one help me.
float f = 2.3455f;
String test = String.format("%.02f", f);
You can also use DecimalFormat
float f = 102.236569f;
DecimalFormat decimalFormat = new DecimalFormat("#.##");
float twoDigitsF = Float.valueOf(decimalFormat.format(f)); // output is 102.24
//double twoDigitsF = Double.valueOf(decimalFormat.format(f)); also format double value
In answer above it always will return 2 deciaml
examples with String.format("%.02f", f)
2.5555 -> 2.55
2.5 -> 2.50
but do you need to get 2.50 for 2.5?
for avoid to get excessive 0, you can use this solution
val format = DecimalFormat("0.##")
return format.format(floatNum)

Categories

Resources