如何将带有$ sing的字符串转换为int

时间:2017-03-16 12:06:09

标签: java selenium automation

我有一个字符串“$ 1,076.00”,我想将它们转换为int, 我捕获一些价值$ 1,076.00并保存在名为originalAmount的字符串中, 并尝试int编辑= Integer.parseInt(originalAmount);它给了我错误java.lang.NumberFormatException:对于输入字符串:“$ 1,076.00”

任何人都可以帮忙吗?

5 个答案:

答案 0 :(得分:0)

你需要删除不需要的部分($ sign),然后仔细解析字符串,因为小数部分是依赖于语言环境的

String pay = "$1,076.00";
NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
Number number = format.parse(pay.replace("$", ""));
double result = number.doubleValue();
System.out.println(result);

答案 1 :(得分:0)

String originalAmount="$1076.00";
String amount = originalAmount.replace($,"");
int edited = Integer.parseInt(amount);

答案 2 :(得分:0)

string sourceString = "$1,076.00";
sourceString.substring(1, sourceString.length() - 1)
int foo = Integer.parseInt(sourceString);

答案 3 :(得分:0)

试试这个:

 String amount = "$1,076,.00";
 String formatted = amount.replace("$", ""); // remove "$" sign
 formatted = formatted.replace(",", ""); // remove "," signs from number

 double amountDouble = Double.parseDouble(formatted); // convert to double
 int amountInt = (int)amountDouble; // convert double value to int
 System.out.println(amountInt); // prints out 1076

答案 4 :(得分:0)

谢谢大家,你的回答对我很有帮助 我想出了

originalAmount = originalAmount.substring(1);

    if (originalAmount.contains("$")) {
        originalAmount = originalAmount.replace("$", "");
    }

    newOriginalAmt = Double.parseDouble(originalAmount);
    System.out.println(newOriginalAmt);
请告诉我你的想法