代码之家  ›  专栏  ›  技术社区  ›  Poorna Senani Gamage

将输入文本值传递给decimalFormat时出错

  •  0
  • Poorna Senani Gamage  · 技术社区  · 7 年前

    我要将输入文本值传递给十进制格式。将值保存为数据库中的十进制格式。但我在代码下面有一个错误。有人能解释代码的错误吗?

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

    java.lang.illegalargumentException:无法将给定对象格式化为 在Java.Text .DeimalFalth.Falm(DeimalFalth.java:507)中的数字 Java.Text .Frase.格式(格式:Java:157)AT Servlet。产品。SaveSuff.DopOST(SaveSuff.Java: 66)

    <input type="text" class="form-control" placeholder="0.00" name="pprice"/>
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Espen S.    7 年前

    这里有几个问题。

    1. 如注释中所述,您不能提供 DecimalFormat.format 方法与A String 价值。它必须是 Number . 这可以通过解析 mf.getParameter("pprice")) 加倍。这样地: decimalformat.format(Double.parseDouble(mf.getParameter("pprice")));
    2. 当你把它转换回双份的时候,这样做根本没有意义。你也可以这么做 product.setPrice(Double.parseDouble(mf.getParameter("pprice"))); . 格式无论如何都会丢失。