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
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 ...
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
Hmm... one way that I can think of is to #include <cmath> then say, call std::fmod(1.234, 1.0)
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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..:-)
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:
The ???? represents where I'm stuck.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; }
I also tried using a loop
Code:while (mean>10) { B=mean%10; }
Edit: Ignore D (Nothing to do with my problems)
It works for smaller numbers: Examples: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; }
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...
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:FLTK: "The most fun you can have with your clothes on.""Are you sure your sanity chip is fully screwed in sir?" (Kryten)
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'"
B needs to be an int value of whatever is after the decimal.
So for 7.4:
A = 7
B= 4 (not 0.4)
Sorry, I'm lost as to what you're asking about now. What are you actually trying to do?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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:FLTK: "The most fun you can have with your clothes on.""Are you sure your sanity chip is fully screwed in sir?" (Kryten)
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'"