java 如何解决“operator * 不能应用于java.lang.string”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35102076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:44:01  来源:igfitidea点击:

How to solve "operator * cannot be applied to java.lang.string"?

javaandroid

提问by Cheong Charlene

I have been trying to convert value to decimal such as RM108.00. I have used the method value = String.format("%.2f", curProduct.price);and it works, but when i want to
apply value * ShoppingCartActivity.getProductQuantity(curProduct), it says that operator * cannot applied to java.lang.string. How do i solve this problem? How should i make the total price into 2 decimal?

我一直在尝试将值转换为十进制,例如 RM108.00。我已经使用了该方法value = String.format("%.2f", curProduct.price);并且它有效,但是当我想
申请时value * ShoppingCartActivity.getProductQuantity(curProduct),它说operator * cannot applied to java.lang.string。我该如何解决这个问题?我应该如何将总价设为 2 位小数?

  item.productPrice = (TextView) convertView
                .findViewById(R.id.textViewTotal);

  value = String.format("%.2f", curProduct.price);

    if (mShowPrice) {
        item.productPrice.setText("Total Price: " +  
  String.valueOf(curProduct.price * 
  ShoppingCartActivity.getProductQuantity(curProduct)));
    } else{
        item.productPrice.setText("RM" + String.valueOf(value));
    }

回答by PearsonArtPhoto

What this means is that one of the two following methods is a string, not a number:

这意味着以下两种方法之一是字符串,而不是数字:

  • curProduct.price
  • ShoppingCartActivity.getProductQuantity(curProduct)
  • curProduct.price
  • ShoppingCartActivity.getProductQuantity(curProduct)

You can convert them to an integer (Or float, if there is a decimal in it) by using the following code:

您可以使用以下代码将它们转换为整数(或浮点数,如果其中有小数):

  • Integer.parseInt(curProduct.price);
  • Float.parseFloat(curProduct.price);
  • Integer.parseInt(curProduct.price);
  • Float.parseFloat(curProduct.price);

It might also be worth looking at where these value are defined, and making sure to use integers/floats whenever possible, as the conversions can take some time. Integers should always be used for whole numbers (If they aren't extremely large), and floats or doubles should be used for decimal numbers. Don't use strings for numbers unless you know exactly what you are doing!

可能还值得查看这些值的定义位置,并确保尽可能使用整数/浮点数,因为转换可能需要一些时间。整数应始终用于整数(如果它们不是非常大),浮点数或双精度数应用于十进制数。除非您确切地知道自己在做什么,否则不要将字符串用于数字!

回答by TeChNo_DeViL

Thats because String.format()gives you a formatted Stringand not any other formatted datatype you expect, and you cannot operate arithmetic operators (such as +) on Strings. Why don't you try to parse all strings to datatypes by using

那是因为String.format()给你一个格式化的String而不是你期望的任何其他格式化的数据类型,并且你不能+在字符串上操作算术运算符(例如)。为什么不尝试通过使用将所有字符串解析为数据类型

  • Double.parseDouble(value)for double
  • Float.parseFloat(value)for float
  • Integer.parseInt(value)for int
  • Double.parseDouble(value)为了 double
  • Float.parseFloat(value)为了 float
  • Integer.parseInt(value)为了 int

and then perform the arithmetic operations.

然后进行算术运算。