Petrus K.
Petrus K.

Reputation: 840

float type-casting isn't working properly, always evaluates to 0.0

code:

double percentage = 0.7711627906976745;
mSweepAngle = (float)(360*(percentage/(double)100));

I've googled around and haven't found an answer, afaik I'm type-casting it correctly yet it keeps evaluating to 0.0. I've even tried typecasting 360, percentage and 100 into float during the calculation but still it doesn't work...

EDIT1: the full code:

package se.kiendys.petrus.da171a.uppg3.budgetapp;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class PieChartView extends View {

    private Paint piechartItem1Paint;
    private Paint piechartItem2Paint;
    private Paint piechartBorderPaint;
    private Paint infoBorderPaint;
    private Paint infoBackgroundPaint;
    private Paint infoTextPaint;
    private RectF oval;

    private int mWidth;
    private int mHeight;
    private int mMarginLeft;
    private int mMarginTop;
    private float mStartAngle;
    private float mSweepAngle;

    private double percentage;

    public PieChartView(Context context) {
        super(context);
        init();
    }

    public PieChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public void init() {
        piechartItem1Paint = new Paint();                   // the piechart items color
        piechartItem2Paint = new Paint();                   // the piechart items color
        piechartBorderPaint = new Paint();                  // the piechart border color
        infoTextPaint = new Paint();                        // the info text color
        infoBackgroundPaint = new Paint();                  // the info background color
        infoBorderPaint = new Paint();                      // the info border color

        mWidth = 200;       // circle width
        mHeight = 200;      // circle height
        mMarginLeft = 10;   // circle and info margin
        mMarginTop = 10;    // circle and info margin
        mStartAngle = 0;    // the starting angle of the arc, begins at the rightmost part of the circle (an angle of 0 degrees correspond to the geometric angle of 0 degrees, or 3 o'clock on a watch)
                            // and through incrementation moves clockwise, the units are degrees

        oval = new RectF(mMarginLeft, mMarginTop, mWidth, mHeight); // sets the shape and size (boundaries) of the drawn circle

        piechartItem1Paint.setAntiAlias(true);
        piechartItem1Paint.setStyle(Style.FILL);
        piechartItem1Paint.setStrokeWidth(0.5f);
        piechartItem2Paint.setAntiAlias(true);
        piechartItem2Paint.setStyle(Style.FILL);
        piechartItem2Paint.setStrokeWidth(0.5f);

        piechartItem1Paint.setColor(0xCCFEFEFE);    // blue color
        piechartItem2Paint.setColor(0xCC343434);    // green color

//      double temp = (360*((double)percentage/(double)100));
//      Log.d(BudgetConstants.DEBUG_TAG, "temp: "+temp);
//      mSweepAngle = (float)temp;
//      Log.d(BudgetConstants.DEBUG_TAG, "init mSweepAngle: "+mSweepAngle);
//      mSweepAngle = (float)(360*(percentage/(double)100));
//      mSweepAngle = (float)(percentage/100.0*360);
        Log.d(BudgetConstants.DEBUG_TAG, "init mSweepAngle: "+mSweepAngle);
    }

    public void setDistributionPercentage(double percentage) {
        this.percentage = percentage;
        Log.d(BudgetConstants.DEBUG_TAG, "percentage: "+percentage);
    }

    protected void onDraw(Canvas canvas) {
        Log.d(BudgetConstants.DEBUG_TAG, "onDraw fired!");

        canvas.drawArc(oval, mStartAngle, mSweepAngle, true, piechartItem1Paint);
        Log.d(BudgetConstants.DEBUG_TAG, "startAngle1: "+mStartAngle+", sweepAngle1: "+mSweepAngle);
        canvas.drawArc(oval, mSweepAngle, (360-mSweepAngle), true, piechartItem2Paint);
        Log.d(BudgetConstants.DEBUG_TAG, "startAngle2: "+mSweepAngle+", sweepAngle2: "+(360-mSweepAngle));
    }

}

Please note that percentage is successfully passed from another activity, currently 0.7711627906976745 is being passed.

Upvotes: 0

Views: 101

Answers (1)

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79875

You're calling init from the constructor, and it's init that's doing this calculation. So no matter what mechanism you're using to set percentage, it's not happening till after the calculation has already been done.

I seriously recommend learning to use a debugger. Stepping through your code would have told you immediately what was happening.

Upvotes: 2

Related Questions