在Textviews上创建百分比值的双精度值

时间:2014-07-07 21:41:12

标签: java android double number-formatting

我的代码更新我的Textviews。我想更新我的textview的一个字符串以正确显示百分比。现在totalDouble返回正确的值,但我无法乘以或除以该值。任何帮助表示赞赏。

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);    
}

任何人都可以帮助我吗?我转换我的数据类型错了吗?

2 个答案:

答案 0 :(得分:2)

尝试使用此代码:

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

现在你应该能够毫无困难地操纵双值。

如果在调用doubleValue时遇到空指针异常,那么您将知道数组中的值为null。您可能想要制作数组:

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

使用值数组而不是对象数组,您将始终拥有一个值,而不是空指针。

答案 1 :(得分:1)

通过使用double值而不是数字,我能够更轻松地更新数组。

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


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