簡體   English   中英

Apache commons CSVPrinter 正在編碼 Double 值

[英]Apache commons CSVPrinter is encoding Double values

我正在嘗試從列表中讀取一些數據並將其寫入 csv 文件而沒有任何格式或編碼。 只是逗號分隔值

但問題是,當我寫入任何值(例如 0.0000666)時,它編碼為 6.66E-5。 我不想要格式。 我想要的輸出是 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();

        } 

將值定義為字符串將在這里工作。 前任:

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

對比

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

所以我想出了真正的問題所在。

如果值 < 1,則 Double 將自身編碼為科學捐贈。因此,每當我在 csv 中打印一些較小的值(如 0.0000666)時,CSVPrinter 都會將其寫為 6.66E-5。

我在將值寫入 csv 之前對其進行了格式化並寫入了格式化的值。 有點像這樣。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM