Thread: Math Help! Converting Doubles

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    3

    Math Help! Converting Doubles

    Alright so here's my problem: I'm doing some math functions that require decimal places, so i'm using double right now to define the varibles. However i'm using an LCD display that requires the information to be in Char format for me to correctly display them. So how can I convert a decimal number, such as 4.294882, to char format? And in that instance i would need each number to be in an array of chars. So i need to convert a 32 bit double number to 8 individual chars so that i can output them. Is it possible? I don't know...but if it is you guys should help!! Thanks a bunch

    ~Mike


    p.s. If this isn't possible that would be good to know too, but i'm pretty sure it is possible...i just wish i knew how to do it!

  2. #2
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Oh, it's definately possible. But I don't know of any library function that can do it for you. You'll have to write your own.

    You'll need to know where the deciaml place is. To do that, you could cast the double as an int and store the number of digits in the result.

    Then, you could use that answer to single out the digits before the decimal point and converting a sinlge digit to ASCII is very simple. Just add 48. When you get to the decimal place, just multiply the double by 10 and cast it into an int until you've taken care of all the digits. Think you get it?
    Code:
    void function(void)
     {
      function();
     }

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    This topic comes up quite a bit... "How do I convert X to Y?" Use a stringstream:

    Code:
    #include <sstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        stringstream sstr;
        double d =  4.294882;
        char cstr[9];
    
        // Store double into string stream
        sstr.precision(7);
        sstr << d;
    
        // Extract sizeof(cstr)-1 characters from the string stream and store in cstr
        sstr.get(cstr,sizeof(cstr));
    
        // Display contents of character array
        cout << cstr << endl;
    
        return 0;
    }
    Depending of the value of the double and how many decimal places it needs to have, you may have to fiddle around with the value passed to the precision function and possibly add a call to the stream's width function as well.
    "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

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    3
    Alright, thanks for those ideas. I was also told that i could use sprintf?
    Code:
    int main(void)
    {
       char display[9];
       double value = 4.294882;
       sprintf(display, "%.8g", value);
       puts(display); /* LCD function */
       return 0;
    }
    That's what someone told me, but my output was 'g' for some reason. Alright i'll try them and let you know, thanks for the help.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    %g is an invalid specifier, I think.
    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
    Join Date
    Jul 2005
    Posts
    3
    Alright, so would you happen to know what the correct specifier would be? I'm not too familiar with sprintf....i have an eight digit double decimal number that i need to get into an array of chars. Thanks!

  7. #7
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    %f. It works for both floats and doubles.
    Code:
    void function(void)
     {
      function();
     }

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Jaken Veina
    %f. It works for both floats and doubles.
    so does %g

    Note that, depending on the magnitude of the number, either %f or %g can result in more chars than indicated by the specifier.

    Regards,

    Dave
    Last edited by Dave Evans; 07-06-2005 at 02:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math
    By knightjp in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 04-01-2009, 05:36 PM
  2. Converting String to Math Equation
    By draggy in forum C++ Programming
    Replies: 5
    Last Post: 07-10-2005, 06:59 PM
  3. how to use operator+() in this code?
    By barlas in forum C++ Programming
    Replies: 10
    Last Post: 07-09-2005, 07:22 PM
  4. Math Header?
    By Rune Hunter in forum C++ Programming
    Replies: 26
    Last Post: 09-17-2004, 06:39 AM
  5. toughest math course
    By axon in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-28-2003, 10:06 PM