【问题标题】:Apache commons CSVPrinter is encoding Double valuesApache commons CSVPrinter 正在编码 Double 值
【发布时间】:2021-12-15 05:48:03
【问题描述】:

我正在尝试从列表中读取一些数据并将其写入 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();

        } 

【问题讨论】:

    标签: java csv apache-commons opencsv apache-commons-csv


    【解决方案1】:

    将值定义为字符串将在这里起作用。 例如:

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

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

    【讨论】:

      【解决方案2】:

      所以我弄清楚了真正的问题所在。

      如果值

      我在将值写入 csv 并写入格式化值之前对其进行了格式化。 有点像这样。

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

      【讨论】:

        猜你喜欢
        • 2018-07-01
        • 1970-01-01
        • 2012-03-21
        • 2012-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多