# How to only show decimal places and not int?

• 10-19-2012
jbl772
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
• 10-19-2012
laserlight
Hmm... one way that I can think of is to #include <cmath> then say, call std::fmod(1.234, 1.0)
• 10-19-2012
Rehman khan
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..:-)
• 10-20-2012
jbl772
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.
• 10-20-2012
jbl772
I also tried using a loop

Code:

```while (mean>10) { B=mean%10; }```
• 10-20-2012
jbl772
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...
• 10-20-2012
rogster001
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
• 10-20-2012
jbl772
B needs to be an int value of whatever is after the decimal.

So for 7.4:

A = 7
B= 4 (not 0.4)
• 10-20-2012
laserlight
Sorry, I'm lost as to what you're asking about now. What are you actually trying to do?
• 10-20-2012
jbl772
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.
• 10-20-2012
laserlight
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?
• 10-21-2012
rogster001
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.