You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android原价与折扣价计算报错问题解决及Recipe模型更新

解决Android中Recipe模型String类型价格的数值运算错误及扩展计算

问题根源

直接对String类型的price和discountprice执行int discount = p.price - p.discountprice会触发编译错误,因为字符串不支持算术运算,必须先将其转换为数值类型(int/float/double)。

解决方案:更新Recipe模型类

通过在模型类中封装类型转换和计算逻辑,既解决运算错误,又能直接复用差价、折扣率的计算逻辑。

方案1:即时转换计算(适合偶尔计算的场景)

在模型类中提供方法,每次调用时完成字符串转数值并计算:

public class Recipe {
    // 保留后端返回的原始String字段
    private String price;
    private String discountprice;

    // 计算原价与折扣价的差值
    public int getPriceDifference() {
        try {
            int original = Integer.parseInt(price.trim());
            int discount = Integer.parseInt(discountprice.trim());
            return original - discount;
        } catch (NumberFormatException e) {
            // 处理非法数字格式(如空串、非数字字符),避免崩溃
            e.printStackTrace();
            return 0;
        }
    }

    // 计算折扣率(返回百分比,保留一位小数)
    public float getDiscountRate() {
        try {
            float original = Float.parseFloat(price.trim());
            float discount = Float.parseFloat(discountprice.trim());
            if (original == 0) return 0; // 避免除以0
            return (discount / original) * 100;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return 0;
        }
    }

    // 原始字段的Getter/Setter
    public String getPrice() { return price; }
    public void setPrice(String price) { this.price = price; }
    public String getDiscountprice() { return discountprice; }
    public void setDiscountprice(String discountprice) { this.discountprice = discountprice; }
}

方案2:初始化时预转换(适合频繁计算的场景)

在设置字符串字段时就完成转换,存储为数值类型,后续计算无需重复转换,提升效率:

public class Recipe {
    private String priceStr;
    private String discountPriceStr;
    // 预转换后的数值字段
    private int price;
    private int discountPrice;

    public void setPrice(String priceStr) {
        this.priceStr = priceStr;
        try {
            this.price = Integer.parseInt(priceStr.trim());
        } catch (NumberFormatException e) {
            this.price = 0;
            e.printStackTrace();
        }
    }

    public void setDiscountPrice(String discountPriceStr) {
        this.discountPriceStr = discountPriceStr;
        try {
            this.discountPrice = Integer.parseInt(discountPriceStr.trim());
        } catch (NumberFormatException e) {
            this.discountPrice = 0;
            e.printStackTrace();
        }
    }

    // 直接用预转换的数值计算
    public int getPriceDifference() {
        return price - discountPrice;
    }

    public float getDiscountRate() {
        if (price == 0) return 0;
        return ((float) discountPrice / price) * 100;
    }

    // Getter方法
    public String getPriceStr() { return priceStr; }
    public int getPrice() { return price; }
    public String getDiscountPriceStr() { return discountPriceStr; }
    public int getDiscountPrice() { return discountPrice; }
}

在AdapterHomeRecipes中使用

无需再手动处理类型转换,直接调用模型方法即可:

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    Recipe recipe = recipeList.get(position);
    // 获取差价
    int priceDiff = recipe.getPriceDifference();
    holder.priceDiffTv.setText("立减¥" + priceDiff);
    // 获取折扣率并格式化显示
    float discountRate = recipe.getDiscountRate();
    holder.discountRateTv.setText(String.format("省%.1f%%", discountRate));
}

关键注意事项

  • 数值类型选择:如果价格包含小数(如19.99元),需用float或double替代int,避免精度丢失。
  • 异常处理:必须捕获NumberFormatException,防止因后端返回非法格式字符串导致崩溃。
  • 除以0防护:计算折扣率时要判断原价是否为0,避免算术异常。

内容的提问来源于stack exchange,提问作者user22830924

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 05:45:15