Thread: Converting Float to Integer

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    1

    Question Converting Float to Integer

    I am really new to programming and am trying to modify an existing program, what I would like to know is if it is possible to convert a floating point number to a whole integer number. For example if I have 123.456 I want to convert it to 123456. I have seen some examples where they simply round the float up or down, but this will not work for me. I need the whole number but basically without any decimal point.

    Any help appreciated.

  2. #2
    KingoftheWorld
    Guest
    First you should convert the float number to
    a string.(numbers to characters) then
    you can store it in an array, then
    parse it (eg. using loop) to find where is the
    decimal point(.)locate, then start count how
    many number after the decimal point. Finally
    if there ,for example, 3 numbers after decimal point then multiply the float number by 1000.
    if 2 numbers after decimal point then multiply
    the float number by 100, and so on....
    Hope you got an idea. There are several way
    of algorithms to deal with it.

    KingoftheWorld

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Working with floats is very fuzzy. For example a float with value 123.456 can be stored in memory like 123.4559999998. That's the reason why you should never compare floats. You can get the same problem when trying to convert a float to an integer the way you want to do it.

    Here is a small example, but it's very messy and it doesn't always work.

    It works fine when I convert the number 123456001, but when I try 123.45601, the result is: 123.456009.

    Code:
    #include <string.h>
    
    int main ()
    {
       float a = 123.456001;
       int result = 0;
       char buf[40];
       char *ptr;
    
       /* copy float number to string (is it %f ???, not sure) */
       sprintf(buf, "%f", a); 
    
       /* find the dot */
       if((ptr = strchr(buf, '.')) == NULL) return -1; 
    
       /* remove the dot */
       for(; *ptr != '\0'; ptr++) *ptr = *(ptr+1); 
    
       /* remove trailing zero's */
       for(ptr-=2; *ptr == '0'; ptr--) *ptr = 0; 
    
       /* copy string to integer (can also use atoi) */
       sscanf(buf, "%d", &result); 
       printf("%d\n", result);
       
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help me
    By warthog89 in forum C Programming
    Replies: 11
    Last Post: 09-30-2006, 08:17 AM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Display list not displaying?
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 09-19-2004, 06:47 PM
  4. error declaration terminated incorrectly help
    By belfour in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2002, 09:07 PM
  5. converting string to float
    By twhubs in forum C Programming
    Replies: 1
    Last Post: 09-16-2001, 09:02 AM