简体   繁体   English

Java字符串到双转换

[英]Java string to double conversion

I've been reading up on the net about the issues with handling float and double types in java. 我一直在网上阅读有关在Java中处理floatdouble类型的问题。 Unfortunately, the image is still not clear. 不幸的是,图像仍然不清楚。 Hence, i'm asking here direct. 因此,我在这里直接问。 :( :(

My MySQL table has various DECIMAL(m,d) columns. 我的MySQL表具有各种DECIMAL(m,d)列。 The m may range from 5 to 30. d stays a constant at 2. 所述m的范围可从5至30 d在2保持恒定。

Question 1. 问题1

What equivalent data-type should i be using in Java to work (ie store, retrieve, and process) with the size of the values in my table? 我应该在Java中使用哪种等效的数据类型来处理(即存储,检索和处理)表中值的大小? (I've settled with double - hence this post). (我已经解决了两倍-因此这篇文章)。

Question 2. 问题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 the dpuField . 是的,问题出在getText()报告dpuField值错误。 This method is called on the JTextField keyTyped event. JTextField keyTyped事件上调用此方法。 So what's going wrong here? 那么,这里出了什么问题?

EDIT 2 编辑2

Looking at : http://journals.ecs.soton.ac.uk/java/tutorial/post1.0/ui/keylistener.html Apparently, keyTyped() does not give me the keycode. 查看: http : keyTyped()显然, keyTyped()不会给我键码。 I'll have to switch to keyRealeased() 我将不得不切换到keyRealeased()

What equivalent data-type should i be using in Java to work (ie store, retrieve, and process) with the size of the values in my table? 我应该在Java中使用哪种等效的数据类型来处理(即存储,检索和处理)表中值的大小? (I've settled with double - hence this post). (我已经解决了两倍-因此这篇文章)。

Since it's a DECIMAL field, you should prefer java.math.BigDecimal . 由于这是一个DECIMAL字段,因此您最好使用 java.math.BigDecimal You can store it in DB using PreparedStatement#setBigDecimal() and you can retrieve it from DB using ResultSet#getBigDecimal() . 您可以使用PreparedStatement#setBigDecimal()将其存储在数据库中,也可以使用ResultSet#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. 也许只是不返回您希望返回的数据,或者您没有使用/调试您希望它们返回的值。

  1. if you need exact precision without rounding errors, you should use a BigDecimal . 如果您需要精确的精度而不舍入错误,则应使用BigDecimal

  2. Your code looks OK - could it be that dpuField.getText() somehow cuts the last character from the string values you list above? 您的代码看起来dpuField.getText() -可能是dpuField.getText()以某种方式从您在上面列出的字符串值中剪切了最后一个字符吗?

Update : you say 更新 :你说

Yes, the problem lies with getText() reporting the wrong value of the dpuField . 是的,问题出在getText()报告dpuField值错误。 This method is called on the JTextField keyTyped event. JTextField keyTyped事件上调用此方法。

Could it be that getText() returns the value of the field before the last typed key is actually appended to it? 难道getText()在最后一个键入的键实际附加到它之前返回了字段的值?

For decimal, I believe you risk losing precision if you don't use a BigDecimal on the Java side, as some decimal fractions can't be stored as a binary fraction. 对于十进制,如果您不在Java端使用BigDecimal ,我相信您可能会失去精度,因为某些十进制分数不能存储为二进制分数。

Prefer Double.valueOf(String) over the constructor, but that's a valid way. 最好使用Double.valueOf(String)而不是构造方法,但这是一种有效的方法。 Something else must be going on (ie I doubt those are the actual String values you're passing in). 必须继续进行其他操作(即,我怀疑这些是您要传递的实际String值)。

Question1: It's bad idea to map DECIMAL columns to Double, usually the BigDecimal is the correct type. 问题1:将DECIMAL列映射到Double是个坏主意,通常BigDecimal是正确的类型。 http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html#1055175 http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html#1055175

Question 2: You are doing something wrong; 问题2:您做错了什么; print the String value before converting. 在转换之前打印String值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM