1

Hi i have a CSV file that contains rows like this

Jacop , "Assistant",150,75

i want to convert 3rd column to 150.75 in java

Should i write a function for this .Or is there another way?

thanks

1

3 Answers 3

1

Quick idea:

  • get that value as String
  • replace "," with "." in String
  • try to parse double with Double.parseDouble
Sign up to request clarification or add additional context in comments.

Comments

1

UBe careful of floating point arithmetic errors, but this is the right idea:

int a = Integer.valueOf("150"); // Since your numbers are coming from a CSV
int b = Integer.valueOf("75");

double c = c = (a * 100 + b) / 100.0;
=> 150.75

Note, the value if 100 is there assuming that your number to the right of the decimal is always two digits. So, if that isn't constant, don't use this answer.

Comments

0

Simple regular expression search and replace in your text file (using sed or whatever) to convert the last comma to a decimal point should work fine.

Then use opencsv to pull it all in:

http://opencsv.sourceforge.net/

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.