How to evaluate a float to first decimal place

This is a discussion on How to evaluate a float to first decimal place within the C Programming forums, part of the General Programming Boards category; Hi, id like to know how to evaluate a float to the first decimal place, i considered converting the float ...

  1. #1
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37

    How to evaluate a float to first decimal place

    Hi, id like to know how to evaluate a float to the first decimal place, i considered
    converting the float to a char and then evaluating it that way, however i think there
    is probably a way of doing this that isn't as extravagant. I want to do this as i am
    writing something that converts denary to binary. Any feedback would be
    greatly appreciated.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you want the integer part, e.g out of 47.11, you want 47. Or do you want the integer part AND the first decimal place, e.g. 47.1?

    Assuming also that the integral value is within the range of an integer, you could use an integer to get the integral part of a number. To get the integral part and an additional decimal, you can get the whole, then subtract that from the float, and multiply the result by ten - the integer part of this is the first decimal place.

    --
    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.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    The function fmod() may help you to do this. http://www.cplusplus.com/reference/c...math/fmod.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,027
    Do you need to do math with the rounded values, or just display them? If you need to do math, use fmod as dwks suggests, or use a fixed point math library. If you just need to display a rounded result, you need to specify the precision of the number to the output stream.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    If you just need to display a rounded result, you need to specify the precision of the number to the output stream.
    For example:
    Code:
    printf("%.1f", 3.14);  /* prints 3.1 */
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37
    Thanks for the replies. Sorry I worded it slightly wrong, the first decimal place is needed, for example with 156.76, i'd need the 8. The integer part is irrelevant. If you are perplexed as to why the decimal place is needed, skim the first part of this http://www.btinternet.com/~a.morris7...uter-Maths.pdf

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by redruby147 View Post
    Thanks for the replies. Sorry I worded it slightly wrong, the first decimal place is needed, for example with 156.76, i'd need the 8. The integer part is irrelevant. If you are perplexed as to why the decimal place is needed, skim the first part of this http://www.btinternet.com/~a.morris7...uter-Maths.pdf
    Nothing in that handout requires this sort of thing, but if you want to do it, more power to you.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    $ cat ./fmodtest.c
    #include <stdio.h>
    #include <math.h>
    
    int main() {
        double x = 156.76;
        double int_part;
        int first = (int)(modf(x, &int_part) * 10 + 0.5);
        printf("First decimal place of %f is %d\n", x, first);
        return 0;
    }
    $ ./fmodtest
    First decimal place of 156.760000 is 8
    $
    Note that I've used modf(), not fmod()! I always get those two confused.

    Also, if you're going to be dealing with negative numbers as well as positive ones, you'll have to use a more sophisticated rounding scheme than I have used above.

    [edit] I suppose I should have read the handout first. Are you trying to convert arbitrary real numbers into binary?

    BTW, I think that "decimal" might be a more common term than "denary", though they both mean the same thing, base ten. Just in case someone comes up to you and starts talking about "decimal".
    [/edit]
    Last edited by dwks; 01-27-2009 at 01:50 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37
    ....wow! *hugs dwks* Thanks, this is exactly what i was looking for .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class won't call
    By Aalmaron in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2006, 04:57 PM
  2. My attempt at lighting math
    By psychopath in forum Game Programming
    Replies: 11
    Last Post: 03-29-2005, 11:35 PM
  3. Multidimensional String
    By Beast() in forum C Programming
    Replies: 14
    Last Post: 07-03-2004, 12:47 AM
  4. Repetition in do{}while
    By Beast() in forum C Programming
    Replies: 25
    Last Post: 06-16-2004, 10:47 PM
  5. Replies: 10
    Last Post: 06-12-2002, 03:15 PM

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