Place values

Pages: 12
Feb 12, 2010 at 4:51pm
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 Feb 12, 2010 at 4:54pm
Feb 12, 2010 at 5:31pm
Change line 8 to
double d = c / 100.0;
100 is an integer. int / int = int. 100.0 is double. int / double = double.
Feb 12, 2010 at 6:37pm
Let's end this
1
2
3
double truncate_to_two_decimal_places(double val) {
    return floor(val * 100.0) / 100.0;
}
Feb 13, 2010 at 1:35am
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 Feb 13, 2010 at 1:37am
Feb 13, 2010 at 1:40am
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