Place values

Pages: 12
It prints 345 with no decimal;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

double placevalue (double a) 
{
    double b = a * 100;
    int c = b;
    double d = c / 100;
    return (d);
}

int main () {
    double x = 345.2359745;
    double y;
    y = placevalue (x);
    cout << "#:" << y;
    cin.get();
    return 0;
}
#:345


Last edited on
Change line 8 to
double d = c / 100.0;
100 is an integer. int / int = int. 100.0 is double. int / double = double.
Let's end this
1
2
3
double truncate_to_two_decimal_places(double val) {
    return floor(val * 100.0) / 100.0;
}
why has nobody stated the obvious yet?

double x= 345.2359745;

printf ("%0.3d", x);

it doesn't change the actual variable, but only chops off the end parts on output.
Last edited on
I think the op wanted to change the actual thing though. setprecision was brought up in the beginning of the discussion.
Topic archived. No new replies allowed.
Pages: 12