Thread: how could I get each digit in a float into a array of ints?

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    118

    how could I get each digit in a float into a array of ints?

    Lets say I have
    Code:
            float a = 12.50f;
    and I want to have
    Code:
            int b[4] = { 1, 2, 5, 0 };
    I already have a way to know how many digits the float has if you are interested:
    Code:
            n = 10;
    	while (1)
    	{
    		Digits++;
    		if (n > TheFloat)
    			break;
    		n *= 10;
    	}
            Digits += 2;
    the last part is for the 2 decimals.
    Thanks for any help.
    Why drink and drive when you can smoke and fly?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    By far the best way is to convert the number to a string and then assign each digit to an int array. Here's a quickie example:
    Code:
    #include <iomanip>
    #include <iostream>
    #include <sstream>
    #include <string>
    
    using namespace std;
    
    int
    main()
    {
      float        a = 12.50f;
      int          b[4];
      char         ch;
      stringstream ss;
    
      ss<< fixed << setprecision(2) << a;
      for (int i = 0; ss>> ch; ) {
        if (ch == '.') {
          continue;
        }
        b[i++] = ch - '0';
      }
      for (int i = 0; i < 4; i++) {
        cout<< b[i] <<endl;
      }
    
      return 0;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    thanks but I am using an old compiler and I dont seem to have the sstream library, if anyone knows of a way to just get like the leftmost digit of the float, then the next digit and so on it would be great. But thanks anyway prelude.
    Why drink and drive when you can smoke and fly?

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    this method uses stdlib.h
    http://www.cplusplus.com/ref/cstdlib/ecvt.html

    if u want free standard compiler go to www.bloodshed.net
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >But thanks anyway prelude.
    I'm not done yet though. You can do it the old fashioned way too:
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int
    main()
    {
      float        a = 12.50f;
      int          b[4];
      int          i, j;
      char         buf[256];
      const int    ndigits = 4;
    
      sprintf(buf, "%f", a);
      for (i = j = 0; j < ndigits; i++) {
        if (buf[i] == '.') {
          continue;
        }
        b[j++] = buf[i] - '0';
      }
      for (i = 0; i < 4; i++) {
        cout<< b[i] <<endl;
      }
    
      return 0;
    }
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    thanks very very much prelude and alphaoid, I now have what I was looking for and I can sleep in peace
    Why drink and drive when you can smoke and fly?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Float Function Issues
    By GCNDoug in forum C++ Programming
    Replies: 5
    Last Post: 10-29-2007, 03:25 PM
  2. Need help with program
    By HAssan in forum C Programming
    Replies: 8
    Last Post: 06-10-2007, 08:05 PM
  3. Reflective Factory Design
    By Shamino in forum Game Programming
    Replies: 4
    Last Post: 12-16-2005, 06:50 PM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  5. Multidimensional String
    By Beast() in forum C Programming
    Replies: 14
    Last Post: 07-03-2004, 12:47 AM