Have you tried this?
float onerm = (float) (weigth/(1.0278-(.0278*reps)));
Question
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.
Solution
Have you tried this?
float onerm = (float) (weigth/(1.0278-(.0278*reps)));
OTHER TIPS
What about this approach?
Float.valueOf(String.valueOf(your_double_variable));
Casting from a double to a float can be done like this:
double d = 1.2;
float f = (float) d;
Or if you just need to express 1.2
as a float to start with, then use 1.2f
.
Note
float
is single-precision 32-bit and double
is double-precision 64-bit so it is possible to lose precision in the conversion. See the primitive data types documentation.In my case you have a Double
variable, you can call Double.floatValue()
like this:
Double deg = 45.0d;
float floatDeg = deg.floatValue();
The issue is that the double
type is higher precision than float
. You'll lose information when you make that assignment. That's why the compiler doesn't let you do that. It's similar to what happens when you want to do char b = a
where a is an int
.
Using an explicit cast might allow you to compile, but data/precision loss could happen.
EDIT:
Since in your original question all the inputs to the calculation are floats
, use float literals. For example, 1.0278
is a double literal. I.e. append f
to the constants such that all the calculations are floats and are not promoted to doubles:
float onerm = weight/(1.0278f-(.0278f*reps));