MadsterMaddness
MadsterMaddness

Reputation: 735

Double values to create percentage values on Textviews

My code to update my Textviews. I want to update one of my textview's strings to show the percentage correctly. Right now totalDouble returns the correct value but I cannot multiply or divide the value. Any help is appreciated.

private static Integer intNumberOfMaxGBsAccepted; 

protected void updateTheDailyandTotalUsageInformation(String string) {

    int daySelected = Integer.parseInt(string); //getting the day as a string

    //converting the series array to type Number
    Number dailyNumber = series2Numbers[daySelected-1]; 
    Number totalNumber = series1Numbers[daySelected-1];


    //converting the type Number to type Double
    Double dailyDouble =  (Double) dailyNumber;
    Double totalDouble = (Double) totalNumber;

    String dailyString = String.format("%.2f", dailyDouble);
    String totalString = String.format("%.2f", totalDouble);



    if(intNumberOfMaxGBsAccepted >=1) { 
        System.out.println(totalDouble);

        // **Inside this if statement I want to do
        // (totalDouble/intNumberOfMaxGBsAccepted) * 100
        // to create a percentage**

     // **I want to create something like this**
     // Double doublePercent = (totalDouble/(double)intNumberOfMaxGBsAccepted) *100;
     // String percent =  doublePercent+"%";
     //usagePlanPercentTextView.setText(percent);



    }

    dailyUsageTextView.setText(dailyString);
    totalUsageTextView.setText(totalString);    
}

Can anyone help me out? Am I converting my data types wrong?

Upvotes: 1

Views: 161

Answers (2)

MadsterMaddness
MadsterMaddness

Reputation: 735

By using double values instead of numbers I was able to update the arrays much easier.

  //converting the type Number to type double
  double dailyDouble = dailyNumber.doubleValue();
  double totalDouble = totalNumber.doubleValue();


  double[] series2Numbers = new double[ {arraysize} ];

Upvotes: 1

AgilePro
AgilePro

Reputation: 5598

try using this instead:

//converting the type Number to type double
double dailyDouble = dailyNumber.doubleValue();
double totalDouble = totalNumber.doubleValue();

Now you should be able to manipulate the double values without difficulty.

If you get a null pointer exception on the call to doubleValue then you will know that the value in the array was null. You might want to make the array:

double[] series2Numbers = new double[ {arraysize} ];

Using an array of values instead of array of objects you will always have a value, and never a null pointer.

Upvotes: 2

Related Questions