CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

how to change , into .

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
dynamitron



Joined: 18 Mar 2009
Posts: 38

View user's profile Send private message

how to change , into .
PostPosted: Tue May 08, 2012 3:07 pm     Reply with quote

I have a code which write a string to a SD card. The file on the SD card has the extension .csv to be easily readable by excel
Before to write I use "sprintf" to convert numerical data into char.

My question : I want that the conversion of the temperature [0] from float to string generates a "," in place of a "." for the decimal separator.

how is it possible to do that ?
Thanks for the answers

Code:
        sprintf(line,"%02u:%02u:%02u %02u/%02u/%02u %6.2f\r\n",heure, Minute,Seconde,day,month,year,temperature [0]);
        write_sd(file_name,line);
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue May 08, 2012 3:21 pm     Reply with quote

If I understand it right..
you want to convert 1.234 to 1,234

Easy enough to do a simple if comma then replace with period function but...

The problem I see is that when you import into say Excel as a dotCSV file, Excel will split the temperature reading 1,234 into two ( 1 and 234).

Is this what you really want ?

Perhaps an example or two would be helpful.
dynamitron



Joined: 18 Mar 2009
Posts: 38

View user's profile Send private message

PostPosted: Tue May 08, 2012 3:39 pm     Reply with quote

For example the value of the float is 21.21
If I use the above code, after the sprintf, I find 21.21 in the result but I would like to have 21,21
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 08, 2012 4:16 pm     Reply with quote

He wants European notation for the decimal point.
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue May 08, 2012 5:44 pm     Reply with quote

Yeah, but that'll get messed up when imported as a Comma Separated Variable file unless he can use a different separater than a comma.
I don't use Excel to often though I am real familiar with Visicalc.
jeremiah



Joined: 20 Jul 2010
Posts: 1358

View user's profile Send private message

PostPosted: Tue May 08, 2012 5:58 pm     Reply with quote

I thought that if you encase it in quotes that it retains the comma? It's been a while with csv format files.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue May 08, 2012 6:33 pm     Reply with quote

actually its EZ

LET the float be formatted with the decimal point

excel has the OPTION after importing the .CSV data
to change the representation format

inside the spreadsheet is the place to do it......
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed May 09, 2012 1:50 am     Reply with quote

OK. So let Excel do the donkey work.

If he really HAS to convert to using a comma there's no problem importing into Excel, since there's an option to change the separator to semicolon, tab and/or any other character.

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Wed May 09, 2012 2:21 am     Reply with quote

Yes. This is very much related to the problem another user was having a while back, where 123,45 inside a command, was returning '45'. In C itself, the decimal separator _must_ be '.'.

The key is to read up on CSV. In CSV files, there are two possible legal 'cell' separators. The ',', and the ';'. (Many programs allow others, but these are the defined ones) In the CSV definitions, _if_ your country locale uses ',' as the decimal separator, then you are meant to use ';' as the cell separator. Using double inverted commas round the numbers (effectively outputting then as strings), sometimes works, but can also give problems with some applications. Quite a few programs take the 'CSV is a separate standard' approach, and whatever the local decimal separator, use '.' inside the CSV file, and convert to the local standard when this is read. Personally this seems to give less problems...
So, if you want to be compatible with other programs, the 'safest' approach seems to be to always use . as the decimal separator.
Alternatively, use ',', and switch to using ';' as the cell separator. In the code you post, you are using ':' as the separator, and quite a few programs won't support this.

If you must output using the comma, then use sprintf, to put the number into a text string, and then use:
Code:

char buffer[20]; //long enough for your number
float float_val=123.456;

sprintf(buffer,"%3.4f",float_val);
*(char *)(strchr(buffer,'.'))=',';


This then gives you the string 123,456 in 'buffer', available to be printed to your output. The cast to 'char *', is not really necessary, but ensures it works with all CCS versions.

Best Wishes
dynamitron



Joined: 18 Mar 2009
Posts: 38

View user's profile Send private message

PostPosted: Wed May 09, 2012 4:48 am     Reply with quote

Thank you all, I will use your answers to find the easiest solution for me.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group