Click to See Complete Forum and Search --> : Need 0.013487 but I have 1.3487e-02


phoenixbr
August 24th, 2005, 11:20 PM
// Matrizo: Dynamic Float Matrix
// i, j : integer;
// pFile: FILE*

...
fprintf (pFile, "%i %i %g\n",i,j,matrizo[i][j])
...



My temp.txt show me 1.3487e-02 but I need 0.013487.

Someone can help me plz?

Tks ^^

Hacker2
August 24th, 2005, 11:40 PM
fprintf (pFile, "%i %i %f\n",i,j,matrizo[i][j])

phoenixbr
August 24th, 2005, 11:56 PM
fprintf (pFile, "%i %i %f\n",i,j,matrizo[i][j])

Hacker2 tks for your answer ^^

But I really need the shorter version, with %f I'll have "0" after the number, thats why I'm using %g.

Kheun
August 25th, 2005, 12:11 AM
You can specify the number of decimal place to be displayed.


fprintf (pFile, "%i %i %0.6f\n",i,j,matrizo[i][j]);

Hacker2
August 25th, 2005, 06:12 PM
Here is a complete spec of all the ways you can specify formatting
http://www.cplusplus.com/ref/cstdio/printf.html

phoenixbr
August 26th, 2005, 12:25 AM
You can specify the number of decimal place to be displayed.


fprintf (pFile, "%i %i %0.6f\n",i,j,matrizo[i][j]);

Kheun, I really need the numbers without "0", the way you told me will have sometimes unecessary "0" after:

Ex.:

0.500000
0.256000
...

But tks very much for your answer ;)

phoenixbr
August 26th, 2005, 12:28 AM
Here is a complete spec of all the ways you can specify formatting
http://www.cplusplus.com/ref/cstdio/printf.html

Tks very much Hacker2 ^^...

I saw the page and I dont think there is a way to show the way I need.. I'll have to go by the "hard" way..

Tks everyone :)

Hacker2
August 26th, 2005, 07:24 AM
You could remove the trailing zeros manually:

char temp[100];
sprintf(temp, "%f",matrizo[i][j]);

int k = strlen(temp);

if(k > 0) k--; //avoid zero-length string problems

while(temp[k]=='0' && k > 0){
temp[k]=0;
k--;
}

fprintf (pFile, "%i %i %s\n",i,j,temp);