简体   繁体   English

Apache commons CSVPrinter 正在编码 Double 值

[英]Apache commons CSVPrinter is encoding Double values

I am trying to read some data from a list and writing that into a csv file without any formatting or encoding.我正在尝试从列表中读取一些数据并将其写入 csv 文件而没有任何格式或编码。 Just comma separated values只是逗号分隔值

but the problem is, when I am writing any value such as 0.0000666, it encoded to 6.66E-5.但问题是,当我写入任何值(例如 0.0000666)时,它编码为 6.66E-5。 I want no formatting.我不想要格式。 My desired output is 0.0000666 should be written as 0.0000666 only.我想要的输出是 0.0000666 应该只写成 0.0000666。

// getting a list in input

    try (BufferedWriter bufferedWriter = Files.newBufferedWriter(Paths.get(fileName));
                CSVPrinter csvPrinter = CSVFormat.DEFAULT
                        .withEscape(Character.MIN_VALUE)
                        .withQuoteMode(QuoteMode.NONE)
                        .withHeader(HEADERS)
                        .withIgnoreHeaderCase()
                        .withTrim()
                        .print(bufferedWriter);) {
            
            csvPrinter.printRecords(list);

            bufferedWriter.flush();
            bufferedWriter.close();
            csvPrinter.close();

        } 

defining the value as a string will work here.将值定义为字符串将在这里工作。 ex:前任:

 printer.printRecord(1, "john73", "John", "Doe", "0.0000666");

vs对比

 printer.printRecord(1, "john73", "John", "Doe", 0.0000666);

So I figured it out what in real was the problem.所以我想出了真正的问题所在。

Double encodes itself into scientific donations if the value < 1. So whenever I was printing some smaller value like 0.0000666 in csv the CSVPrinter was writing it as 6.66E-5.如果值 < 1,则 Double 将自身编码为科学捐赠。因此,每当我在 csv 中打印一些较小的值(如 0.0000666)时,CSVPrinter 都会将其写为 6.66E-5。

I formatted the value before writing it to csv and wrote the formatted values.我在将值写入 csv 之前对其进行了格式化并写入了格式化的值。 Someting like this.有点像这样。

String.format("%.4f", 6.66E-5)

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

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