java - 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.
解决方案
将您的替换,
为.
:
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();
}
}
推荐阅读
- amazon-web-services - AWS ECS Fargate:应用程序负载均衡器返回 503 并带有奇怪的模式
- sql - 有没有办法在 PostgreSQL 函数中查询当前行值以及来自同一表的过滤结果集
- r - 如何对文件夹中的所有文件应用相同的转换
- swift - 如何在两个日期之间获取每个星期五的日期?
- python - 如何在 Plotly 3D 散点图中设置点标记的样式/格式?
- python - 没有公共列时如何使用 Pandas 连接两个 CSV 文件
- php - Pass Parameter to Relationship Function or alternative solution
- macos - Mac App - 每次鼠标点击的日期时间戳
- mysql - 在 Python 3 中通过 SQL Alchemy 缓慢插入 CloudSQL
- java - Java Blackjack 程序创建错误:非静态变量 this 不能从静态上下文中引用