Java字符串进行双重转换 [英] Java string to double conversion
问题描述
我已经在网上阅读关于在java中处理 float
和 double
类型的问题的问题。不幸的是,形象还不清楚。因此,我直接问这里。 :(
我的 MySQL
表有各种 DECIMAL(m,d)
列, m
的范围可以为5到30. d
保持在2的常数。 p>
问题1。
我应该在Java中使用什么类似的数据类型来工作(即存储,并且处理)与我的表中的值的大小(我已经结算了双倍,因此这个帖子)。
问题2。
在尝试从字符串中解析一个double时,我收到错误
Double dpu = new Double(dpuField.getText());
例如 -
1 - > java.lang.NumberFormatException:empty String
pre>
10 - > 1.0
101 - > 10.0
101. - > 101.0
101.1 - > 101.0
101.19 - > 101.1
我做错了什么?将字符串转换为双重值的正确方法是什么?
我应该采取什么措施执行这些值的操作?
编辑
这是代码 -
System.out.println(dpuField.getText());
Double dpu = new Double(dpuField.getText());
System.out.println(dpu);
是的,问题出在
getText()
dpuField 。
此方法在JTextField keyTyped
事件中调用。那么这里有什么问题?
编辑2
看看:
http://journals.ecs.soton.ac.uk/java /tutorial/post1.0/ui/keylistener.html
显然,keyTyped()
不给我键码。我必须切换到keyRealeased()
解决方案
我在Java中使用什么等效的数据类型来工作(即存储,检索和处理)与表中的值的大小? (我已经结算了两次,因此这个帖子)。
由于它是一个
DECIMAL
字段,您应该喜欢java.math.BigDecimal
。您可以使用PreparedStatement#setBigDecimal()
,您可以使用ResultSet#getBigDecimal()
。
在尝试从字符串中解析一个double时,我收到错误
这不可能是真的。这个问题在别的地方。也许只是没有返回你希望返回的数据,或者你没有使用/调试你期望的值。
I've been reading up on the net about the issues with handling
float
anddouble
types in java. Unfortunately, the image is still not clear. Hence, i'm asking here direct. :(My
MySQL
table has variousDECIMAL(m,d)
columns. Them
may range from 5 to 30.d
stays a constant at 2.Question 1.
What equivalent data-type should i be using in Java to work (i.e store, retrieve, and process) with the size of the values in my table? (I've settled with double - hence this post).
Question 2.
While trying to parse a double from a string, i'm getting errors
Double dpu = new Double(dpuField.getText());
for example -
"1" -> java.lang.NumberFormatException: empty String "10" -> 1.0 "101" -> 10.0 "101." -> 101.0 "101.1" -> 101.0 "101.19" -> 101.1
What am i doing wrong? What is the correct way to convert a string to a double value? And what measures should i take to perform operations on such values?
EDIT
This is the code -
System.out.println(dpuField.getText()); Double dpu = new Double(dpuField.getText()); System.out.println(dpu);
Yes, the problem lies with
getText()
reporting the wrong value of thedpuField
. This method is called on theJTextField keyTyped
event. So what's going wrong here?EDIT 2
Looking at : http://journals.ecs.soton.ac.uk/java/tutorial/post1.0/ui/keylistener.html Apparently,
keyTyped()
does not give me the keycode. I'll have to switch tokeyRealeased()
解决方案What equivalent data-type should i be using in Java to work (i.e store, retrieve, and process) with the size of the values in my table? (I've settled with double - hence this post).
Since it's a
DECIMAL
field, you should preferjava.math.BigDecimal
. You can store it in DB usingPreparedStatement#setBigDecimal()
and you can retrieve it from DB usingResultSet#getBigDecimal()
.While trying to parse a double from a string, i'm getting errors
This can't be true. The problem lies somewhere else. Maybe it is just not returning the data you expect to be returned or you are not using/debugging the values you expect them to be.
这篇关于Java字符串进行双重转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!