有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将印度货币转换为双倍?

我想把印度货币兑换成双倍。下面是我的代码:

String initalAmount = "2341";
Format format = NumberFormat.getCurrencyInstance(new Locale("en", "in"));
String convertedAmountToINR = format.format(new BigDecimal(initalAmount));
// it prints Rs. 2,341.00

现在,我想把它转换成DOUBLE-like2341.00。我试过几种方法让它工作,但没有


共 (3) 个答案

  1. # 1 楼答案

    请像这样试试

    double doubleINR=Double.parseDouble(convertedAmountToINR);
    

    对于您的情况,请尝试以下内容:

      double d=Double.parseDouble(convertedAmountToINR.replaceAll("Rs.|,", ""));
    
  2. # 2 楼答案

    您应该将initialAmount转换为双精度,如下所示:

    Double.valueOf(initalAmount);
    

    无法转换convertedAmountToINR的原因是其中的“Rs.”无法转换为double

  3. # 3 楼答案

    如果确实需要,可以使用相同的format对象将convertedAmountToINR转换回:

    NumberFormat format = NumberFormat.getCurrencyInstance(new Locale("en", "in"));
    double value = format.parse(convertedAmountToINR).doubleValue();
    

    这将仅分析使用此format对象格式化的字符串