Java中的数字格式例外,但格式为真
List<Person> list=new ArrayList<Person>();
public List<Person> readFile(){
File file =new File("dosya.txt");
try {
FileReader fileReader=new FileReader(file);
BufferedReader reader=new BufferedReader(fileReader);
while(reader.readLine()!=null){
String personData= reader.readLine();
System.out.println(personData);
Person person=new Person();
person.setName(personData.substring(1,personData.indexOf("#")));
String sayı=personData.substring(personData.indexOf("#")+1,personData.indexOf("#",personData.indexOf("#")+1));
person.setMoney(Double.valueOf(sayı));
personData=personData.substring(personData.indexOf("#")+1);
System.out.println(personData);
person.setRate(Double.valueOf(personData.substring(0,3)));
personData=personData.substring(personData.indexOf("#")+1);
System.out.println(personData);
person.setNumber(Short.valueOf(personData));
list.add(person);
}
} catch (Exception e) {
e.printStackTrace();
}
这是我的代码,它分开“ fsyxaimvct wdyeyttuye#1528640,88#0,7#9” 这种类型的数据,它给出了MI错误,这样
java.lang.NumberFormatException: For input string: "2273370,68"
at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.base/java.lang.Double.parseDouble(Double.java:549)
at java.base/java.lang.Double.valueOf(Double.java:512)
at FileProcess.readFile(FileProcess.java:19)
at Main.main(Main.java:7)
为什么它不能转动2273370,68兼法
List<Person> list=new ArrayList<Person>();
public List<Person> readFile(){
File file =new File("dosya.txt");
try {
FileReader fileReader=new FileReader(file);
BufferedReader reader=new BufferedReader(fileReader);
while(reader.readLine()!=null){
String personData= reader.readLine();
System.out.println(personData);
Person person=new Person();
person.setName(personData.substring(1,personData.indexOf("#")));
String sayı=personData.substring(personData.indexOf("#")+1,personData.indexOf("#",personData.indexOf("#")+1));
person.setMoney(Double.valueOf(sayı));
personData=personData.substring(personData.indexOf("#")+1);
System.out.println(personData);
person.setRate(Double.valueOf(personData.substring(0,3)));
personData=personData.substring(personData.indexOf("#")+1);
System.out.println(personData);
person.setNumber(Short.valueOf(personData));
list.add(person);
}
} catch (Exception e) {
e.printStackTrace();
}
This is my code and it seperate "fsyxaimvct wdyeyttuye#1528640,88#0,7#9"
this type of data and it gives mi error like that
java.lang.NumberFormatException: For input string: "2273370,68"
at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.base/java.lang.Double.parseDouble(Double.java:549)
at java.base/java.lang.Double.valueOf(Double.java:512)
at FileProcess.readFile(FileProcess.java:19)
at Main.main(Main.java:7)
Why this cant turn 2273370,68 to double
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
double.valueof()
不'do'locale-它专门解析使用。
作为小数分隔符的数字。您的输入字符串包含一个逗号,这意味着它将失败。但是,此代码本质上是没有用的。您不能用双打进行货币 - 双打不精确,会静静地围绕。这不是您使用货币时想要的。
最简单的解决方案是为货币存储“原子单位”。对于美元,存储美元。对于比特币,请存储Satoshis。对于日元,日元。对于英磅,存储便士。等等。在
长
中,或者如果必须,则biginteger
。或者,使用库(
joda-currency
是一个很好的库)。您经常会听到建议使用bigdecimal
,但这通常是一个坏主意(这有各种各样的毛茸茸的并发症;例如,除非您真的知道自己是什么做 - 为他们编写代码很复杂)。用例如
“ -1234,58”
来解析字符串,例如“ -1234,58”
,可以解析为long
-123458
lust:尝试使用
double.parsedouble
将使您陷入困境,然后随机失去或获得一分钱。运行这样的银行,您可能会遇到一些麻烦:)Double.valueOf()
doesn't 'do' locale - it parses specifically only numbers that use.
as decimal separator. Your input string contains a comma which means it'll fail.But, this code is essentially useless. You can't do currency with doubles - doubles are imprecise and will silently round. This is not what you want when working with currency.
The simplest solution is to store the 'atomic unit' for the currency. For dollars, store dollarcents. For bitcoin, store satoshis. For yen, store yen. For english pounds, store pennies. And so on. In a
long
, or if you must, aBigInteger
.Alternatively, use a library (
joda-currency
is a good one). You'll often hear advice to useBigDecimal
but this is usually a bad idea (it's got all sorts of hairy complications; for example, You can't divide BigDecimals at all unless you really know what you are doing - and it's complicated to write code for them).Parsing a string with e.g. a euro amount such as
"-1234,58"
in it can be parsed to thelong
-123458
using:Any attempt to use
Double.parseDouble
will get you into that rounding trouble and you randomly lose or gain a cent. Run a bank like that and you might get into a wee bit of trouble :)