rounding down

How can I round down without declaring a value as an int? I tried setting the precision to 0 but it rounds to the nearest figure(ex 1.5 = 2 or 1.4 = 1) I want (1.5 = 1). I also tried setting the variables to integers but then it affects my answer for the distance question(energyTotal).

The first part of the problem solves for Psec and the second part of the question solves for energyTotal. I'm trying to get the answers to print as integers(rounded down to the nearest whole number) but when I do this it affects the calculation for energyTotal. This is because I need the un-rounded value to solve for the energyTotal, which is why I changed them from int to double.

Is there a way to round down to the nearest whole number with setting the variables as doubles and using setprecision?

If not, then if I set both variables as int; then how can i use the un-rounded number of "Psec" to get calculated into "energyTotal"?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <iomanip>
#include <math.h>       

using namespace std;

int main()
{
    double M,Mb,v, CfDraft, Pair,k,Cr,g, Proll, timeTravel, energyTotal, Psec;
    int distance;
    cout<<"Enter the mass of the rider "<<endl;
    cin>>M;
    cout<<"Enter the mass of the bike "<<endl;
    cin>>Mb;
    cout<<"Enter the velocity "<<endl;
    cin>>v;
    cout<<"Enter the coefficient of drafting "<<endl;
    cin>>CfDraft;

    k = 0.18;
    Cr = 0.001;
    g = 9.8;
    Pair = (k*CfDraft)*pow(v,3);
    Proll = Cr*g*(M+Mb)*v;
    Psec = Pair + Proll;
    //cout<<Psec<<" W"<<endl;
    cout<<fixed<<setprecision(0)<<Psec<<" W"<<endl;
    cout<<"Enter the distance in kilometers for the rider to travel "<<endl;
    cin>>distance;
    timeTravel = (distance*1000)/v;
    energyTotal = Psec * timeTravel;
    //cout<<energyTotal<<endl;
    cout<<fixed<<setprecision(0)<<energyTotal<<endl;
    
    /*This is how it looks when I set Psec and energyTotal to int:
    double M,Mb,v, CfDraft, Pair,k,Cr,g, Proll, timeTravel;
    int energyTotal, Psec, distance;
    cout<<"Enter the mass of the rider "<<endl;
    cin>>M;
    cout<<"Enter the mass of the bike "<<endl;
    cin>>Mb;
    cout<<"Enter the velocity "<<endl;
    cin>>v;
    cout<<"Enter the coefficient of drafting "<<endl;
    cin>>CfDraft;

    k = 0.18;
    Cr = 0.001;
    g = 9.8;
    Pair = (k*CfDraft)*pow(v,3);
    Proll = Cr*g*(M+Mb)*v;
    Psec = Pair + Proll;
    cout<<Psec<<" W"<<endl;
    //cout<<fixed<<setprecision(0)<<Psec<<" W"<<endl;
    cout<<"Enter the distance in kilometers for the rider to travel "<<endl;
    cin>>distance;
    timeTravel = (distance*1000)/v;
    energyTotal = Psec * timeTravel;
    cout<<energyTotal<<endl;
    //cout<<fixed<<setprecision(0)<<energyTotal<<endl;
    */ 
    


return 0;
}
Last edited on
If you want to always round down (i.e., towards negative infinity) see:
http://en.cppreference.com/w/cpp/numeric/math/floor

You may find this post interesting:
http://www.cplusplus.com/forum/articles/3638/
thank you! "floor" solved it
Topic archived. No new replies allowed.