1

I am reading a following CSV file in java and

1191196800.681 - !AIVDM,1,1,,,13aG?N0rh20E6sjN1J=9d4<00000,0*1D

Here is my code to read this CSV file and convert the 1st column of the CSV file (which is a String) to a double

public class ReadCSV {

public static void main(String[] args) {

    try {
        BufferedReader CSVFile = new BufferedReader(new FileReader(
                "C:\\example.txt"));

        try {
            String dataRow = CSVFile.readLine();

            while (dataRow != null) {
                String[] dataarray = dataRow.split("-");

                String temp = dataarray[0];

                double i = Double.parseDouble(temp);

                System.out.println(temp);
                System.out.println(i);
                dataRow = CSVFile.readLine();

            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

}

}

When I execute the code I get the following values

1191196800.681 
1.191196800681E9

Why is the value of variable "temp" and "i" not the same. What changes must I do in the code to get the following values:

1191196800.681 
1191196800.681 

Thanks in advance.

2
  • 1
    Don't print it in the default scientific notation? Try printf/format. Commented May 19, 2012 at 14:41
  • 1
    1.191196800681E9 == 1191196800.681 Commented May 19, 2012 at 14:42

3 Answers 3

7

temp and i are exactly the same, just the double value is formatted in scientific notation. You can format it differently if you like using DecimalFormat for instance.

Sign up to request clarification or add additional context in comments.

Comments

1

Yups vizier is correct what you need to do is this:

public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
    String dataRow = "1191196800.681";


        String[] dataarray = dataRow.split("-");

        String temp = dataarray[0];
        DecimalFormat df = new DecimalFormat("#.###");
        double i = Double.parseDouble(temp);

        System.out.println(temp);
        System.out.println(df.format(i));

}

Comments

0

The other result is in scientific notation, but is the same.

If you want change the representation you can use String.format("%.3f", i), which represents the number with 3 decimals

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.