【问题标题】:Error when passing the input text value to DecimalFormat将输入文本值传递给 DecimalFormat 时出错
【发布时间】:2018-11-20 09:38:01
【问题描述】:

我想将输入文本值传递为十进制格式。将值以十进制格式保存在数据库中。但我在代码下方出现错误。谁能解释一下代码的错误。

DecimalFormat decimalformat = new DecimalFormat("#.00");
String dp = decimalformat.format(mf.getParameter("pprice"));
product.setPrice((Double.parseDouble(dp)));

java.lang.IllegalArgumentException:无法将给定的对象格式化为 java.text.DecimalFormat.format(DecimalFormat.java:507) 处的数字 java.text.Format.format(Format.java:157) 在 Servlet.product.save_product.doPost(save_product.java:66)

<input type="text" class="form-control" placeholder="0.00" name="pprice"/>

【问题讨论】:

标签: java string decimalformat


【解决方案1】:

这里有几个问题。

  1. 如 cmets 中所述,您不能为 DecimalFormat.format 方法提供 String 值。它必须是Number 的子类型。这可以通过将mf.getParameter("pprice")) 的响应解析为双精度来解决。像这样: decimalformat.format(Double.parseDouble(mf.getParameter("pprice")));
  2. 当您之后将其转换回 Double 时,这样做完全没有意义。你也可以这样做 product.setPrice(Double.parseDouble(mf.getParameter("pprice")));。无论如何,格式都会丢失。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    相关资源
    最近更新 更多