Java序列化双精度数

发布于 2025-01-02 07:59:14 字数 160 浏览 4 评论 0 原文

我想将双精度数序列化/反序列化为可读字符串。我很高兴失去超过 6 个 dp 的任何准确性。

这样做的正确方法是什么?

我担心 System.out.println(1.03 - .42) 打印 0.6100000000000001 而不是 0.61。

谢谢。

I want to serialize/deserialize doubles to a readable String. I'm happy to lose any accuracy beyond the 6th dp.

Whats the correct approach for doing this?

I'm concerned about System.out.println(1.03 - .42) printing 0.6100000000000001 and not 0.61.

Thxs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

陌伤浅笑 2025-01-09 07:59:14

如果您想要该值用于测试目的,Double.toString(double) 可能会很好。

如果您想要一个更具可读性的数字(例如 1.01 而不是 1.01000000000587732),您可以使用 DecimalFormat (http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html) 或 BigDecimal :

BigDecimal bd = BigDecimal.valueOf(double)
BigDecimal bd2 = bd.setScale(6, RoundingMode.HALF_UP); // six decimal
String out = bd2.toString();
String out2 = bd2.toPlainString();

编辑:用户编辑后,我认为正确的方法是:

    DecimalFormat df = new DecimalFormat("0.00");
    System.out.println(df.format(1.03 - .42));

If you want the value for testing purposes, Double.toString(double) could be good.

If you want a more readable number (1.01 instead of 1.01000000000587732, for example), you can use DecimalFormat (http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html) or BigDecimal:

BigDecimal bd = BigDecimal.valueOf(double)
BigDecimal bd2 = bd.setScale(6, RoundingMode.HALF_UP); // six decimal
String out = bd2.toString();
String out2 = bd2.toPlainString();

Edit: after the user edit, I think a correct approach would be:

    DecimalFormat df = new DecimalFormat("0.00");
    System.out.println(df.format(1.03 - .42));
木格 2025-01-09 07:59:14

最简单的方法是使用 PrintWriter 和 BufferedReader。

double[][] doubles = new double[10][10];
for(double[] ds : doubles)
    for(int i=0;i<ds.length;i++)
        ds[i] = Math.random() * 100;

PrintWriter pw = new PrintWriter("double.tsv");
for(double[] ds : doubles) {
    for(double d: ds)
        pw.printf("%.6f\t", d);
    pw.println();
}
pw.close();

BufferedReader br = new BufferedReader(new FileReader("double.tsv"));
String line;
while((line = br.readLine())!=null) {
    String[] words = line.split("\t");
    for (String word : words) {
        double d = Double.parseDouble(word);
        System.out.print(d+"\t");
    }
    System.out.println();
}

印刷

72.520491   85.341994   21.958533   48.914986   14.959155   54.398293   54.438528   6.265907    77.654032   94.363109   
65.594713   66.943443   69.891651   3.62663 13.445234   3.281239    99.897356   53.072258   11.931774   83.802643   
17.387541   56.598334   90.104328   69.379567   95.631605   34.931461   73.336693   21.300139   50.511963   79.326218   
61.844333   20.076352   81.668206   69.500067   45.936303   80.639298   86.534122   51.031074   89.718252   89.510961   
41.923934   31.450051   35.373463   82.607984   69.796802   11.387551   28.684281   28.524173   3.926274    27.390139   
43.116206   81.455006   52.424004   46.399187   84.572294   20.368705   7.414759    18.389408   91.835487   96.594918   
87.632357   63.293224   8.751766    70.693449   12.30385    41.090964   93.36716    31.281827   95.411907   29.825814   
46.516716   53.442007   18.273036   15.306335   87.773823   72.392803   85.34191    78.388259   80.203328   19.306142   
93.249744   50.981212   7.02211 90.461556   46.307283   0.304891    33.055868   98.374818   33.050704   92.423133   
41.791121   84.102962   37.881277   80.713237   24.856496   53.619386   99.447379   62.681776   14.927559   37.969094   

The simplest approach is to use PrintWriter and BufferedReader.

double[][] doubles = new double[10][10];
for(double[] ds : doubles)
    for(int i=0;i<ds.length;i++)
        ds[i] = Math.random() * 100;

PrintWriter pw = new PrintWriter("double.tsv");
for(double[] ds : doubles) {
    for(double d: ds)
        pw.printf("%.6f\t", d);
    pw.println();
}
pw.close();

BufferedReader br = new BufferedReader(new FileReader("double.tsv"));
String line;
while((line = br.readLine())!=null) {
    String[] words = line.split("\t");
    for (String word : words) {
        double d = Double.parseDouble(word);
        System.out.print(d+"\t");
    }
    System.out.println();
}

prints

72.520491   85.341994   21.958533   48.914986   14.959155   54.398293   54.438528   6.265907    77.654032   94.363109   
65.594713   66.943443   69.891651   3.62663 13.445234   3.281239    99.897356   53.072258   11.931774   83.802643   
17.387541   56.598334   90.104328   69.379567   95.631605   34.931461   73.336693   21.300139   50.511963   79.326218   
61.844333   20.076352   81.668206   69.500067   45.936303   80.639298   86.534122   51.031074   89.718252   89.510961   
41.923934   31.450051   35.373463   82.607984   69.796802   11.387551   28.684281   28.524173   3.926274    27.390139   
43.116206   81.455006   52.424004   46.399187   84.572294   20.368705   7.414759    18.389408   91.835487   96.594918   
87.632357   63.293224   8.751766    70.693449   12.30385    41.090964   93.36716    31.281827   95.411907   29.825814   
46.516716   53.442007   18.273036   15.306335   87.773823   72.392803   85.34191    78.388259   80.203328   19.306142   
93.249744   50.981212   7.02211 90.461556   46.307283   0.304891    33.055868   98.374818   33.050704   92.423133   
41.791121   84.102962   37.881277   80.713237   24.856496   53.619386   99.447379   62.681776   14.927559   37.969094   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文