首页 > 解决方案 > Parse string to float/double

问题描述

I am getting values from jTable and putting it in my DataBase (MySQL). In my DB table there is column PAYMENT that is double. When I try to put values from my jTable there is a problem with it:

String payment=(String) jTable1.getValueAt(row, 2);
double pay=double .parseDouble (payment);
........
pst.setDouble(3, zar);

I am getting this exception:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "84,21834324"

How can I put this column from jTable as double in my DB table? P.S.: I tried with float too.

标签: javadatabaseparsingdouble

解决方案


将您的替换,.

String payment = (String) jTable1.getValueAt(row, 2);
double pay = Double.parseDouble(payment.replace(",". "."));
........
pst.setDouble(3, zar);

编辑——如果你想支持更复杂的值:

import java.util.Locale;
import java.text.*;

class Main {
  public static void main(String[] args) throws ParseException {
    String v1 = "84,21834324";
    String v2 = "1.234.567,89";

    NumberFormat format = NumberFormat.getInstance(Locale.ENGLISH);
    Number n1 = format.parse(v1);
    Number n2 = format.parse(v2);

    double d1 = n1.doubleValue();
    double d2 = n2.doubleValue();
  }
}

从这个问题中解除了编辑的解决方案


推荐阅读