CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2005
    Posts
    30

    Need 0.013487 but I have 1.3487e-02

    Code:
    // 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 ^^

  2. #2
    Join Date
    Jul 2004
    Posts
    142

    Re: Need 0.013487 but I have 1.3487e-02

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

  3. #3
    Join Date
    Apr 2005
    Posts
    30

    Re: Need 0.013487 but I have 1.3487e-02

    Quote Originally Posted by Hacker2
    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.

  4. #4
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: Need 0.013487 but I have 1.3487e-02

    You can specify the number of decimal place to be displayed.

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

  5. #5
    Join Date
    Jul 2004
    Posts
    142

    Re: Need 0.013487 but I have 1.3487e-02

    Here is a complete spec of all the ways you can specify formatting
    http://www.cplusplus.com/ref/cstdio/printf.html

  6. #6
    Join Date
    Apr 2005
    Posts
    30

    Re: Need 0.013487 but I have 1.3487e-02

    Quote Originally Posted by Kheun
    You can specify the number of decimal place to be displayed.

    Code:
    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

  7. #7
    Join Date
    Apr 2005
    Posts
    30

    Re: Need 0.013487 but I have 1.3487e-02

    Quote Originally Posted by Hacker2
    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

  8. #8
    Join Date
    Jul 2004
    Posts
    142

    Re: Need 0.013487 but I have 1.3487e-02

    You could remove the trailing zeros manually:
    Code:
    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);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured