finding the decimal part of a number

This is a discussion on finding the decimal part of a number within the C++ Programming forums, part of the General Programming Boards category; suppose i have a number such as 3.4, but i have no idea what the number is, aside from the ...

  1. #1
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116

    finding the decimal part of a number

    suppose i have a number such as 3.4, but i have no idea what the number is, aside from the fact that i know its a positive number greater than 1 with both a whole part and a decimal part. how can i figure out the decimal part without modifing the number.
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    You could do a function with a for loop. Then as the for loop iterates. See if the i in the for loop is the same as your other number. Im diggin my self into a hole so ive mad a little example.
    Code:
    float getDecimal(float theDecimal)
    {
    	for (int i = 0; i < 1000; i++)
    	{
    		if (theDecimal == (i \ 100 )
    			return i;
    	}
    }
    Now I have not checked this but it should work. All you have to do to get a bigger range is adjust the i < 1000 value

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    if you don't know what the number is, and you want to find the part after the decimal point..

    you can use a double to string function (you can use the Int to String function found in the programming FAQ and just replace the ints with doubles).

    then use the find() fxn in the string class to locate the decimal.

    eh, here's a quick example..

    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    using namespace std;
    
    string double_str(double num);
    
    int main()
    {
    	double number = 4.657;
    	string str_d = double_str(number);
    	
    	//cout << str_d;
    	//used to test double_str fxn
    	
    	int found = str_d.find('.');
    	string temp = str_d.substr(found + 1, str_d.length() - found);
    	// you can just use str_d.substr(found, str_d.length() - found);
    	// if you want to keep the decimal in the cout
    	
    	cout << temp;
    
    	return 0;
    }
    
    string double_str(double num)
    {
    	ostringstream myStream;
    	myStream << num << flush;
    	
    	return (myStream.str());
    }
    I think this is what you are looking for. it's dirty and the variables could be named better, but it's just a quick example..

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    double Double = 3.14152;
    int whole = Double;
    double decimal = Double - whole;
    cout << decimal << endl;

    you might get a warning assigning a double to an int, in which case you might want to cast the double to an int before assignment.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    148
    Code:
    double modf(
       double x,
       double *intptr 
    );
    Splits a floating-point value into fractional and integer parts.
    #include <cmath>

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    ooh, there are easier ways.. geeze, i need to get programming regularly again..

  7. #7
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116
    Code:
    double modf(
       double x,
       double *intptr 
    );
    can u give me an example of how i would use this? pointers always confuse me...
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >can u give me an example of how i would use this?
    Code:
    #include <iostream>
    #include <cmath>
    
    int main()
    {
        double f = 123.456;
        double integral;
        double fractional;
    
        fractional = modf(f, &integral);
    
        std::cout<<"Fractional part: "<< fractional <<std::endl;
        std::cout<<"Integral part:   "<< integral <<std::endl;
    }
    My best code is written with the delete key.

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    why don't you do this:

    Code:
    ...
    digit=int(number);   //truncates the decimal
    decimal=number%number;   //the remainder will be the decimal
    ...
    I think that should work, and it doesn't really modify the number... this way the decimal becomes a whole number
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    The '%' operator doesn't work with doubles and floats. Floats have a different set of commands than int's and chars. And dividing a number by itself is always 1, with no remainder.

  11. #11
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116
    ok thx everyone, especially wedge and prelude, that will work. i appreciate your help

  12. #12
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Originally posted by ygfperson
    The '%' operator doesn't work with doubles and floats. Floats have a different set of commands than int's and chars. And dividing a number by itself is always 1, with no remainder.
    oh yeah... I forgot about that... yeah, scratch that... modulous only works with int's...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  13. #13
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    sigh

    Code:
    double blah = 3.14159265358979323; //I know most of those will be truncated....
    double decimal_only = blah - (int)blah;
    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  14. #14
    Registered User
    Join Date
    Jul 2003
    Posts
    7
    cmath is filled with useful math functions. *gasp* what a surprise! Anyways, here's a nice place to look them all up. As for your question, the "floor" function is what you'd want.

    http://www.cplusplus.com/ref/cmath/index.html

    ~SK

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number of digits in a decimal number
    By maverix in forum C Programming
    Replies: 7
    Last Post: 11-04-2007, 11:12 AM
  2. How to get the decimal part of a number...
    By Caldus in forum C++ Programming
    Replies: 13
    Last Post: 06-12-2006, 06:41 PM
  3. Finding number of lines of code
    By arron in forum Linux Programming
    Replies: 8
    Last Post: 01-06-2006, 04:35 AM
  4. Replies: 3
    Last Post: 02-25-2002, 10:19 AM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM

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