Java中的数字格式例外,但格式为真

发布于 2025-01-23 06:58:55 字数 1729 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹉夏雨初晴づ 2025-01-30 06:58:55

double.valueof()不'do'locale-它专门解析使用作为小数分隔符的数字。您的输入字符串包含一个逗号,这意味着它将失败。

但是,此代码本质上是没有用的。您不能用双打进行货币 - 双打不精确,会静静地围绕。这不是您使用货币时想要的。

最简单的解决方案是为货币存储“原子单位”。对于美元,存储美元。对于比特币,请存储Satoshis。对于日元,日元。对于英磅,存储便士。等等。在中,或者如果必须,则biginteger

或者,使用库(joda-currency是一个很好的库)。您经常会听到建议使用bigdecimal,但这通常是一个坏主意(这有各种各样的毛茸茸的并发症;例如,除非您真的知道自己是什么做 - 为他们编写代码很复杂)。

用例如“ -1234,58”来解析字符串,例如“ -1234,58”,可以解析为long -123458 lust

Pattern p = Pattern.compile("^(-?)(\\s*\\d*)(?:,(\\d{2}))?$");
var m = p.matcher("2273370,68");
long amount;
if (m.matches()) {
  amount =
    (m.group(1).equals("-") ? -1L : 1L) * (
    100 * (m.group(2).isEmpty() ? 0 : Long.parseLong(m.group(2))) +
    (m.group(3) != null ? Long.parseLong(m.group(3)) : 0L));
} else throw new NumberFormatException();

:尝试使用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, a BigInteger.

Alternatively, use a library (joda-currency is a good one). You'll often hear advice to use BigDecimal 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 the long -123458 using:

Pattern p = Pattern.compile("^(-?)(\\s*\\d*)(?:,(\\d{2}))?
quot;);
var m = p.matcher("2273370,68");
long amount;
if (m.matches()) {
  amount =
    (m.group(1).equals("-") ? -1L : 1L) * (
    100 * (m.group(2).isEmpty() ? 0 : Long.parseLong(m.group(2))) +
    (m.group(3) != null ? Long.parseLong(m.group(3)) : 0L));
} else throw new NumberFormatException();

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文