rounding off floats

This is a discussion on rounding off floats within the C Programming forums, part of the General Programming Boards category; how do i display floats to the significant number of digits entered by user at runtime? suppose user enters a ...

  1. #1
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136

    rounding off floats

    how do i display floats to the significant number of digits entered by user at runtime?

    suppose user enters a float no = 88.8765421
    and he enters the number of significant number of digits to be 2

    then my printf() will become

    printf("%.2f",no);

    if he enters 3 significant number of digits then printf() should become

    printf("%.3f",no);
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    First use sprintf to print the format string as specified by the user input for rounding off, then use that format string in printf to print the floating point value.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    thanks...that was clever
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I think you will have to use a temporary variable created by some precision-setting function, and then just inject that as a plain %f.

    I don't know of a such a function, but the prototype would be:
    Code:
    float setprecision (float number, int places);
    which you should be able to use sprintf() to copy the number into a string, trim/round off to the approriate place, then scanf() to put the modified string back into your return float.

    PING's version is quicker if you don't need a float and can just use the modified sprintf string.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Code:
    printf("%.*f",sig_digits,no);
    Whatever value is in sig_digits will be used to dynamically control the precision.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    Quote Originally Posted by hk_mp5kpdw View Post
    Code:
    printf("%.*f",sig_digits,no);
    Whatever value is in sig_digits will be used to dynamically control the precision.
    damn! k & r had really thought about everything...

    i dont need a work-around after all.

    thanks all!
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Nifty. Last week I learned what * does with an input template (ignore field); I guess you can't use that there. Thanks!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rounding off floats
    By cole in forum C Programming
    Replies: 5
    Last Post: 09-29-2008, 04:25 AM
  2. Reading errors in floats
    By Improvolone in forum C++ Programming
    Replies: 8
    Last Post: 03-21-2006, 02:20 PM
  3. Help with rounding a number
    By nickk in forum C Programming
    Replies: 3
    Last Post: 06-02-2004, 11:44 AM
  4. comparing two arrays of floats
    By COBOL2C++ in forum C++ Programming
    Replies: 7
    Last Post: 07-16-2003, 03:22 AM
  5. Rounding Floats
    By Beginner921 in forum C Programming
    Replies: 5
    Last Post: 02-23-2003, 11:02 AM

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