java - 双值以在 Textviews 上创建百分比值

标签 java android double number-formatting

我更新 TextView 的代码。我想更新我的 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);    
}

谁能帮帮我?我的数据类型转换错了吗?

最佳答案

尝试改用它:

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

现在您应该能够毫无困难地操作 double 值。

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

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

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

关于java - 双值以在 Textviews 上创建百分比值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24620361/

相关文章:

c# - 在 C# 中指定特定的 double 文字

c++ - 在 C++ 中将包含二进制操作的字符串转换为 double

java-8 - DoubleSummaryStatistics 中的 sum 与 simpleSum 与 sumCompensation?拥有它们有什么重要意义?

java - Android 版 Google map 的生产 API key

java - SQLite 更新后获取旧值

android - Firebase 电子邮件验证链接中的深层链接不起作用

java - Android编程——制作URI获取音频位置

java - 有可能减少这个吗? (或 Java 中的解构赋值)

java - 使用注释属性的方面切入点

java嵌套Map数据结构读写操作