In my database, I have a couple of "real" fields.
Heres the structure:
database.execSQL("create table " + TABLE_LOGS + " ("
+ COLUMN_ID + " integer primary key autoincrement,"
+ COLUMN_ID_DAY_EXERCISE + " integer not null,"
+ COLUMN_REPS + " integer not null"
+ COLUMN_WEIGHT + " real not null"
+ COLUMN_1RM + " real not null"
+ COLUMN_DATE + " integer not null"
+ ")");
Now what I am trying to do is calculate 1RM so that I can insert it into the database.
Here is my function so far:
public void createLog(long id_day_exercise, float reps, long weight) {
// create 1rm
// create date timestamp
float onerm = weight/(1.0278-(.0278*reps));
long unixTime = System.currentTimeMillis() / 1000L;
}
I am stuck here. It is giving me the error "cannot convert from double to float" for onerm
. I've tried casting the weight as a float by using (Float) in front of it, I've tried using weight.floatValue() and nothing seems to work.
float is single-precision 32-bit and double is double-precision 64-bit so it is possible to lose precision in the conversion.
Using TypeCasting to Convert Double to Float in Java To define a float type, we must use the suffix f or F , whereas it is optional to use the suffix d or D for double. The default value of float is 0.0f , while the default value of double is 0.0d . By default, float numbers are treated as double in Java.
Java Float doubleValue() method The doubleValue() method of Java Float class returns a double value corresponding to this Float Object by widening the primitive values or in simple words by directly converting it to double via doubleValue() method .
Have you tried this?
float onerm = (float) (weigth/(1.0278-(.0278*reps)));
What about this approach?
Float.valueOf(String.valueOf(your_double_variable));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With