How to only show decimal places and not int?

This is a discussion on How to only show decimal places and not int? within the C++ Programming forums, part of the General Programming Boards category; Hey I'm really stuck on how to show only decimal places in a number. For example, if I had 1.234 ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    6

    How to only show decimal places and not int?

    Hey I'm really stuck on how to show only decimal places in a number.

    For example, if I had 1.234 I would only want to output .234

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,114
    Hmm... one way that I can think of is to #include <cmath> then say, call std::fmod(1.234, 1.0)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2012
    Location
    Chennai, Tamil nadu, India
    Posts
    30
    Yeah... laserlight is correct....

    And The another way is to subtract the floor of the number from the original number..

    I mean, 2.345 - 2 = .345
    say a =2.345
    a - floor(a);

    floor(); is available in math.h header file...

    So no worries..:-)

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    6
    Hmm well basically the user inputs Rmin and Rmax values. Then the mean is computed.

    I have three variables, A B and C.

    A B & C are parts of the mean in scientific notation. For example,

    7.4 x 10^1 (7=A, 4=B, 1=C)



    My code is:


    Code:
    #include <iostream>
    #include <math.h>
    #include <stdio.h>
    using namespace std;
    int main()
    {
        int A, B, C, D;
        double x, y, mean;
            
    		//Entering Values
    		cout << "Enter RMin: ";
            cin >> x;
            cout << "Enter RMax: ";
            cin >> y;
            mean=(x+y)/2;
            
    
    
    		//Print out mean
    		cout << "Mean: " << mean << endl;
    
    
    		//B
    		????????????
    
    
    		//C
    		C=log10(mean);
       
    		//A
            while (mean>10)
            {
            A=mean/=10;
            }
    
    
    		//Printing A B C D
            cout << "A: " << A << endl;
    		cout << "B: " << B << endl;
    		cout << "C: " << C << endl;
    
    
    	return 0;
    }
    The ???? represents where I'm stuck.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    6
    I also tried using a loop

    Code:
    while (mean>10)
    {
    B=mean%10;
    }

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    6
    Edit: Ignore D (Nothing to do with my problems)
    Code:
    #include <iostream>
    #include <math.h>
    #include <stdio.h>
    using namespace std;
    int main()
    {
        int A, B, C, D;
        double x, y, mean;
            
    		//Entering Values
    		cout << "Enter RMin: ";
            cin >> x;
            cout << "Enter RMax: ";
            cin >> y;
            mean=(x+y)/2;
        
    
    
    		//Print out mean
    		cout << "Mean: " << mean << endl;
    
    
    
    
    		//C
    		C=log10(mean);
    
    
    
    
    		//D
    		D=(100*(y-mean))/mean;
    
    
    
    
    		//A
            while (mean>10)
            {
            A=mean/=10;
            }
    
    
    		//B
    		B=(mean - floor(mean))*10;
    
    
     
    
    
    		//Printing A B C D
            cout << "A: " << A << endl;
    		cout << "B: " << B << endl;
    		cout << "C: " << C << endl;
    		cout << "D: " << D << endl;
    
    
    	return 0;
    }
    It works for smaller numbers: Examples:

    Rmin=60
    Rmax=80
    Mean=70
    A=7
    B=0
    C=1
    D=14


    But for bigger numbers:
    Rmin=51000
    Rmax=61000
    Mean=56000
    A=5
    B=5
    C=4
    D=8

    The B value for bigger mean's is always wrong...

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    if you literally only want to show the decimal part then you may as well just convert it to a string and trim off whatever precedes the decimal point- if you need it for something mathematical then your problems remain
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    6
    B needs to be an int value of whatever is after the decimal.

    So for 7.4:

    A = 7
    B= 4 (not 0.4)

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,114
    Sorry, I'm lost as to what you're asking about now. What are you actually trying to do?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Oct 2012
    Posts
    6
    Basically, the user inputs two values which are added and divided by 2 to get the mean.

    Using the mean, the values of A B and C are obtained. For example,

    184 = 1.84 x 10^2 (in scientific notation) - A = 1, B=84 C=2
    74 = 7.4x10^1 - A=7, B=4, C=1

    I'm not sure how to code B.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,114
    I think you may need to use fixed point arithmetic instead of floating point arithmetic, otherwise it may be almost impossible to correctly determine the value of B.

    It may be a good idea to tell us the exact requirements and why you are doing this in the first place. I'm thinking of things like: what is the range of input? Can you read the input as a string or an int instead of a double? Would formatting the input into a string and extracting the parts be okay, as rogster001 suggested?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    in the examples from post #10 you could just use the left hand value to extract A and B at least then you dont have to consider the decimal point - But then we have no idea what your overall aim is.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal places
    By Gordon in forum Windows Programming
    Replies: 9
    Last Post: 06-08-2008, 10:04 AM
  2. Decimal places
    By Gordon in forum Windows Programming
    Replies: 4
    Last Post: 09-28-2007, 11:03 AM
  3. How to remove all decimal places?
    By Rush152 in forum C Programming
    Replies: 4
    Last Post: 05-29-2006, 01:52 AM
  4. Getting Two Decimal Places
    By LostNotFound in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2003, 08:34 PM
  5. decimal places
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 08:08 PM

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