format command of C that will not round up the value

This is a discussion on format command of C that will not round up the value within the C Programming forums, part of the General Programming Boards category; i have a question please refer below.. Example: Code: #include <stdio.h> int main() { printf("%6.1f", 123456.123456); printf("\n%6.2f", 123456.123456); printf("\n%6.3f", 123456.123456); ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Afghanistan
    Posts
    2

    format command of C that will not round up the value

    i have a question
    please refer below..

    Example:
    Code:
    #include <stdio.h>
    int main() {
        printf("%6.1f", 123456.123456);
        printf("\n%6.2f", 123456.123456);
        printf("\n%6.3f", 123456.123456);
        printf("\n%6.4f", 123456.123456);
    }
    output :
    123456.1
    123456.12
    123456.123
    123456.1235

    at last output the result was 123456.1235 because it was rounded up.

    How to display this value by not rounding up?
    like 123456.1234 ?

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Printf is supposed to round it. To prevent this, truncate the argument to 4 decimal places.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You could sprintf() into a temp buffer for 5 decimal places and then lop off the last digit.

    Or, you could multiply the source number by 10,000, cast it to an int, and then back to a float and divide by 10,000.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm sure I already answered this question somewhere else...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    May 2008
    Location
    Afghanistan
    Posts
    2
    Quote Originally Posted by Todd Burch View Post
    You could sprintf() into a temp buffer for 5 decimal places and then lop off the last digit.
    could be better but i have no idea to lop it off.
    in VB we can used MID() function
    but how about in C ?

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    Use Following Code

    Quote Originally Posted by opaw View Post
    i have a question
    please refer below..

    Example:
    Code:
    #include <stdio.h>
    int main() {
        printf("%6.1f", 123456.123456);
        printf("\n%6.2f", 123456.123456);
        printf("\n%6.3f", 123456.123456);
        printf("\n%6.4f", 123456.123456);
    }
    output :
    123456.1
    123456.12
    123456.123
    123456.1235

    at last output the result was 123456.1235 because it was rounded up.

    How to display this value by not rounding up?
    like 123456.1234 ?
    Code:
    #include "stdafx.h"
    
    
    #include <stdio.h>
    int main() 
    {
        printf("%6.4f", 123456.123456);
        printf("\n%6.4f", 123456.123456);
        printf("\n%6.4f", 123456.123456);
        printf("\n%6.4f", 123456.123456);
    
    	return 0;
    }

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by opaw View Post
    could be better but i have no idea to lop it off.
    in VB we can used MID() function
    but how about in C ?
    Code:
    char test[] = "123456.123456";
    
    test[11] = '\0';
    perhaps?

    Of course that's just an example, strchr() may help you.

    or a bit of casting:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        double test = 123456.123456;
        int places = 10 * 10 * 10 * 10,     /* 4 decimal places, 10^4 */
            cast = 0,
            test2 = 0;
    
        /* truncate decimals */
        cast = (int) test;
    
        test2 = (test - cast) * places;
        
        printf("&#37;d.%d\n", cast, test2);
        return 0;
    }
    Again, it's a demo don't use it
    Last edited by zacs7; 05-29-2008 at 06:13 AM.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    Quote Originally Posted by opaw View Post
    could be better but i have no idea to lop it off.
    in VB we can used MID() function
    but how about in C ?
    It is an error in itself to compare VB to C. They are nothing alike and they work nothing alike.
    If you facilities closer to VB, then C++ is your territory. Otherwise you will have to stick to non-objected approaches and functions and low-level stuff.

    Good luck.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Why is everyone making this sound so difficult?

    printf("\n%6.4f", (int)(123456.123456*10000)/10000.0);

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by R.Stiltskin View Post
    Why is everyone making this sound so difficult?

    printf("\n%6.4f", (int)(123456.123456*10000)/10000.0);
    There's an echo in here...
    There's an echo in here...

    Quote Originally Posted by Todd
    Or, you could multiply the source number by 10,000, cast it to an int, and then back to a float and divide by 10,000.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Why is everyone making this sound so difficult?

    printf("\n&#37;6.4f", (int)(123456.123456*10000)/10000.0);
    Have you considered the possibility of integer overflow?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    If you print it to 5 places, the round might travel through the whole number. You would have to print the whole number at full precision to a buffer and then truncate it, I think.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Compression/Decompression Wave File and MP3
    By cindy_16051988 in forum Projects and Job Recruitment
    Replies: 51
    Last Post: 04-29-2006, 06:25 AM
  4. Seeking Format Advice
    By PsychoBrat in forum Game Programming
    Replies: 3
    Last Post: 10-05-2005, 05:41 AM
  5. Please help with some coding..
    By stehigs321 in forum C Programming
    Replies: 2
    Last Post: 10-27-2003, 05:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21