Title: Converting double to char in csv file
Tags: java;string;csv
Question: I have a problem to convert a String number into a char character/csv file:
Using the Filwriter for reading a csv file:
FileWriter writer = new FileWriter("features.csv");
Header of my csv file is:
writer.append("EnergieMagnXYZ");
writer.append(',');
writer.append("label");
writer.append('\n');
Appending rows of data by:
writer.append(Double.toString(energie));
writer.append(',');
writer.append(label);
Creating label values by:
String label = " ";
if(tt[i] &gt; 600){
label = "1";
}else if(tt[i] < 600){
label = "0";
The values of my Energie double array tt[i] are:
2139.0882022271244
2334.38651422678
2329.342679013897
2286.6136318937574
But when I create my csv file my output will be like this:
EnergieMagnXYZ,label
21.390.882.022.271.200,00
233.438.651.422.678,00
2.329.342.679.013.890,00
22.866.136.318.937.500,00
But I want an output like this in my created csv file (in the case label is 0):
EnergieMagnXYZ,label
2139.0882022271244,0
2334.38651422678,0
2329.342679013897,0
2286.6136318937574,0
Be careful:
2139.0882022271244,0
is not single number but 2139.0882022271244 and 0 are seperated fields. So my rows consist of a double value and label in a number format (0 or 1). Moreover the dots in my double value are really important for the further use.
What do I need to change to get an output like this?
Comment: hello. the actual value is 2139.0882022271244. sorry it may be confusing. the 0 after the comma  doesnt belong to the actual value of 2139.0882022271244, its a single value

Comment: Yes! When I have created the csv file and look at the first row the output will be .200,00. As I mentionded what I want is 2139.0882022271244,0

Comment: it strange because when I do the following: writer.append(','); writer.append(label); writer.append(','); writer.append('\n'); then output will be 2139.0882022271244,0, correctly but with a comma at the end

Comment: I have used the Filewriter.

Comment: What is the actual value of your Energie? Is it  21e15? Also, are you sure what you are getting is `.200,00`? I think it might be `.200,0,0`.

Comment: No, I was asking about your current output. Does it really print `.200,00`?

Comment: I don't think this output comes from this part of the code. Perhaps there is something else that writes to your writer? Perhaps some other writer writing to the file?

Comment: Sorry, you'll have to edit your question and add all the relevant code. What is `writer`, where is it initialized, everything else that uses the same writer, etc.


Here is another answer: This is what you are looking for:
NumberFormat nf = NumberFormat.getInstance();
nf.setGroupingUsed(false); // remove the dots grouping each 3 digits
nf.setMaximumFractionDigits(0); // remove the fraction digits
String formatedEnergie = nf.format(Energie));
Now you print formatedEnergie instead of Energie like the following.
NumberFormat nf = NumberFormat.getInstance();
nf.setGroupingUsed(false); // remove the dots grouping each 3 digits
nf.setMaximumFractionDigits(13); // remove the fraction digits
String formatedEnergie = nf.format(Double.toString(energie)));
writer.append(formatedEnergie);
writer.append(',');
writer.append(label);
You will get the value
2286.6136318937574
If that's not exactly what you want, play a little with the NumberFormat API until you get the output you are looking for.
Comment for this answer: thank you for answer. its not exactly what I wanted.probably my described intention hasnt been clear. the expression 21.390.882.022.271.200,0  are two seperated fields in my csv file the 0 after the comma doesnt belong to the number  21.390.882.022.271.200. so i have two fields **21.390.882.022.271.200 (field energie value)**,**0(field label)**
Comment for this answer: thank you so much! its almost working. but when I use **String formatedEnergie = nf.format(Double.toString(energie)))** I'll get an exception "Cannot format given Object as a Number" I have edited the code line in **String formatedEnergie = nf.format(energie)**. Well, the output will be like this: **2139,0882022271244**. Instead of dots I'll get commas. Is there a way to get dots instead of commas?
Comment for this answer: In that case, change the line of the code I wrote above from :
nf.setMaximumFractionDigits(0);
nf.setMaximumFractionDigits(13);
that will give you 13 fraction digits.
So, if your Energie is a double 2139.0882022271244, you will have exactly that.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 8, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:02/11/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><string><csv>', 'Title': 'Converting double to char in csv file', 'ViewCount': 812, 'id': 28464596, 'user_AboutMe': None, 'user_AccountId': 5257426.0, 'user_DisplayName': 'Benaissa Tahiti', 'user_Reputation': 75.0, 'user_Views': 30.0, 'user_id': 4199924.0}}