Click here to Skip to main content
Rate this: bad
good
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,"%f",v);
  if(strlen(res)>14)
  sprintf(res,"%Le",v);// if the result is having more number of digits
  return(res);
 
}
 
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);
  print_result(result_str);
}
 
 
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
v5
Rate this: bad
good
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 | :)
  Permalink  
v2
Comments
ccodeworld.blogspot.com 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
good
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 539
1 Maciej Los 300
2 DamithSL 233
3 Sergey Alexandrovich Kryukov 209
4 BillWoodruff 200
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 28 Sep 2012
Copyright © CodeProject, 1999-2014
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