java - 在 Java 中使用 BufferedWriter 格式化双输出

标签 java format double bufferedwriter

我正在做一项作业,并且已经完成了代码,但是我在正确设置输出格式方面遇到了问题。我正在获取一个文本文件,根据价格分离产品,并将数据附加到两个单独的文件中。如何使用 BufferedWriter 使 double 的格式正确,使其显示为没有美元符号的货币值 (0.00)?它当前输出为 (0.0)。感谢您的帮助。

我的代码:

//import the io utilities and scanner
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.util.Scanner;


/**
 * class ProductData uses the scanner utility to read a file, FileWriter and
 * BufferedWriter to write data from original file to two new files
 * based on the product price.
 */
public class ProductData
{
    /**
     * method main calls the scanner to read a file, uses a while loop
     * to scan through the file until done, and uses two if loops
     * to determine the price and append the data to two new files.
     * @param args
     * @throws java.io.FileNotFoundException
     */
    public static void main(String[] args) throws FileNotFoundException
    {


        //create a scanner, call the file to be read
        Scanner reader = new Scanner(new File("products.txt"));

        //variables defined
        int productID;
        String productDescription;
        double productPrice;


        //while loop to read each line in the file
        while (reader.hasNext())
        {
            //retrieves each item in order using the next feature of the scanner
            productID = reader.nextInt();
            productDescription = reader.next();
            productPrice = reader.nextDouble();

            //if loop for price greater than or equal to 50.00
            if(productPrice >= 50.00)
            {
                try
                {
                    // Create file
                    FileWriter fstream = new FileWriter("WishList.txt",true);
                    //create BufferedWriter to write data to the file
                    BufferedWriter out = new BufferedWriter(fstream);
                    //writes data and blank line to file
                    out.write(productID + " " + productDescription + " " + productPrice );
                    out.newLine();
                    //Close the output stream
                    out.close();
                }

                catch (Exception e)
                {
                    //Catch exception if any
                    System.err.println("Error: " + e.getMessage());
                }

            }

            //if loop for price less than 50.00
            if(productPrice < 50.00)
            {
                try
                {
                    // Create file
                    FileWriter fstream = new FileWriter("GiftIdeas.txt",true);
                    //creates BufferedWriter to write data to file
                    BufferedWriter out = new BufferedWriter(fstream);
                    //writes data and blank line to file
                    out.write(productID + " " + productDescription + " " + productPrice );
                    out.newLine();
                    //Close the output stream
                    out.close();
                }

                catch (Exception e)
                {
                    //Catch exception if any
                    System.err.println("Error: " + e.getMessage());
                }

            }//ends if loop
        }//ends while loop
    }//ends method main
}

最佳答案

例如:

out.write(productID + " " + productDescription + " " + 
String.format("%.2f",productPrice) );

关于java - 在 Java 中使用 BufferedWriter 格式化双输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1152982/

相关文章:

perl - 如何创建数字标尺和均匀间隔的数字宽度标记

Java:将(长)对象转换为 double 的多种方法

java - 一旦外部文本文件为空 JAVA 就停止 BufferedReader

java - 转换 xml 文件时发生 CharConversionException

testing - 如何让 Cargo Test 漂亮地打印失败输出

javascript - JS中如何比较时间

c - C 中双倍的最小公倍数

java - JSON - Java Eclipse java.lang.ClassCastException : Integer cannot be cast to java. lang.Double

Java 无法解析的日期 : different number of characters in ms value

使用 Kryo 序列化对象时出现 java.lang.StackOverflowError