Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot convert from double to float

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.

like image 306
scarhand Avatar asked Jan 25 '13 00:01

scarhand


People also ask

What does it mean Cannot convert from double to float?

float is single-precision 32-bit and double is double-precision 64-bit so it is possible to lose precision in the conversion.

Can we convert double into float?

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.

Can we assign double value to float 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 .


2 Answers

Have you tried this?

float onerm = (float) (weigth/(1.0278-(.0278*reps)));
like image 140
WLin Avatar answered Sep 25 '22 14:09

WLin


What about this approach?

Float.valueOf(String.valueOf(your_double_variable));
like image 39
Ahmad Raza Avatar answered Sep 23 '22 14:09

Ahmad Raza