Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C convert strings
Hi guys,
I just got stuck with conversion of a big floating value to string format.
In my program,

void double_to_string(long double v)
  unsigned char res[40];
// here i am using compiler library function sprintf() which will give us the string format
  sprintf(res,"%Le",v);// if the result is having more number of digits
void fun1()
  long double v;
  unsigned char *result_str;
  v = 0.56726*6.456*1e-12; // based on some calculations i used to get this value
  result_str = double_to_string(v);
When i debug my code, in variable values i found the value of v in double_to_string function like this 3.66223056e-12. But in my sprintf() function giving 0.000000 as result. It's unable to convert such type of value to string. How can i over come this to get the correct results.
Note: My main intention is doing such big calculations and converting them in strings format with more accuracy. (Like our PC calculator)
Due to limitations of my display area, i am choosing exponent results if the resultant string is having more digits.
I hope i mentioned my problem clearly.So, If can any one have solution for this please help me out.Excuse for my poor english.
Posted 27-Sep-12 18:57pm
Edited 27-Sep-12 21:14pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Your arguments to sprintf() are backwards, the output string goes first, then the format statement, then the values to be formatted into the string. See here[^]
Also, the default precision (6) is not usually adequate for very small numbers. anything e-12 is pretty small. Try something like %.20f Smile | :)
Comments at 28-Sep-12 2:16am
Thank u Chuck O'Toole..

Yeah, you are write.. By mistake here i have passed the arguments wrongly.i will rectify it in a moment.

If i use %.20f it's truncating the value after some digits. and more over, i don't have the display area to display 20 digits at a time.

If i have result string with more than 14 digits, i need to display in exponent format.
Is there anyway to achieve this.If it is there, please guide to do..

Thank you.
Chuck O'Toole at 28-Sep-12 10:35am
If you pass the arguments correctly, your statement with %Le should do the trick. Or use the link that I posted in the solution to look at all the ways available for formatting.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Look at the format type specifiers[^] for the one that suits your needs. In your case I would suggest the g code as the best choice.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,455
1 OriginalGriff 8,295
2 Peter Leow 4,839
3 Kornfeld Eliyahu Peter 3,210
4 Maciej Los 2,301

Advertise | Privacy | Mobile
Web04 | 2.8.150327.1 | Last Updated 28 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100