# round .5

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 06-12-2009
msshapira
round .5
Hi,

I'm trying to find a prebuit cpp function (or C) which manipulates output:
for the input of
Code:

```double a = 1.55555555  b= (manipulated a) cout << b;```
i would like the output to be
1.56
in other words, I want set prescision to .2 (with sprintf,or setpressicioon, or whatever)
make sure that .5 is rounded upwards
Is there a buildin function for this in STD of MATH or is this somthing i must write?

Thanks
• 06-12-2009
msshapira
excel FIXED
I just noticed that this is exactly what MS excel does with the FIXED function....
How can I emulate this
• 06-12-2009
hk_mp5kpdw
So do you actually want the value in the variable to change or do you simply want to display the already existing value to the specifications you've given (without actually changing the value stored in the variable)?

This will simply display the value in the variable as you've requested without really changing it:
Code:

```#include <iomanip> ... cout << fixed << setprecision(2) << b;```
• 06-12-2009
Sebastiani
Code:

```double round( double v, int p = 0 ) {         static const double                 t = 10,                 h = 0.5;         double                 s = pow( t, p );         v *= s;         double                 w = floor( v ),                 f = v - w;         if( f >= h )                 ++w;         v = w / s;         return v;        }```
Example:
round( 555.555, 2 ) == 555.56
round( 555.554, 2 ) == 555.55
round( 555.554 ) == 556
round( 555.555, -2 ) == 600
round( 545.555, -2 ) == 500
• 06-12-2009
dwks
You may also be interested in: http://cboard.cprogramming.com/cplus...le-number.html

@Sebastiani: Very nice code! Just two notes:
Code:

```\$ cat round.cpp #include <iostream> #include <cmath> using std::pow; using std::floor; double my_round( double v, int p = 0 ) {         static const double                 t = 10,                 h = 0.5;         double                 s = pow( t, p );         v *= s;         double                 w = floor( v ),                 f = v - w;         if( f >= h )                 ++w;         v = w / s;         return v; } int main() {     std::cout << my_round(-1.5) << std::endl;     return 0; } \$ ./round -1 \$```
The first is that you always round upwards, whereas I think -1.5 should be rounded downwards, should it not? Also, there's a standard function called "round" so unless you use a different name, you'll probably get compiler errors (unless you don't #include <cmath>, which is difficult since the code uses pow and floor).

Oh, and I'd have used more descriptive variable names . . . .
• 06-12-2009
brewbuck
Multiply by 100.0, round to nearest integer, divide by 100.0
• 06-12-2009
dwks
Yeah, that's probably the way I would do it (if I actually needed the rounded value for other calculations, that is). Details of "round to nearest integer" are in that thread I linked to.

For completeness (since the OP mentioned sprintf()), here's how you would do it with printf() (sprintf() is a simple modification from here):
Code:

`printf("%.2f\n", 3.14159);  /* prints 3.14 */`
• 06-12-2009
ಠ_ಠ
Quote:

Originally Posted by brewbuck
Multiply by 100.0, round to nearest integer, divide by 100.0

That would just add 2 steps to what is needed to round anyway
• 06-12-2009
dwks
Ah, but rounding to the nearest integer is very easy to do. Here's an example of mine from that thread that I posted which I wish everyone would read (:D): http://cboard.cprogramming.com/cplus...tml#post865912

Basically: modf is one easy way to do it.
• 06-12-2009
brewbuck
Quote:

Originally Posted by ಠ_ಠ
That would just add 2 steps to what is needed to round anyway

And your more efficient proposed method for rounding to the N'th decimal place looks like what?
• 06-12-2009
Sebastiani
>> The first is that you always round upwards, whereas I think -1.5 should be rounded downwards, should it not?

Apparently, the answer to that is "it depends" (I believe mine would be the "round half up asymmetric" algorithm, as used in Java). :p The choice seems a bit arbitrary, though, since 0.5 is exactly in the middle (and so either way could be considered a correct approach), although the way you suggested is probably more consistent.

>> Also, there's a standard function called "round" so unless you use a different name, you'll probably get compiler errors (unless you don't #include <cmath>, which is difficult since the code uses pow and floor).

I'm an idiot. I completely forgot that was a standard function!

>> Oh, and I'd have used more descriptive variable names . . . .

I figured it might be a fairly common homework question so I decided to make it somewhat more obscure (although not obfuscated).
• 06-12-2009
Sebastiani
>> Multiply by 100.0, round to nearest integer, divide by 100.0

But doesn't that just truncate the value (without rounding)?

EDIT:
Ignore that. I'm having trouble understanding english today. :p
• 06-12-2009
ಠ_ಠ
Quote:

Originally Posted by brewbuck
And your more efficient proposed method for rounding to the N'th decimal place looks like what?

actually, the way you said to do it is the only way I can see to do it

oops
• 06-12-2009
dwks
Quote:

Apparently, the answer to that is "it depends" (I believe mine would be the "round half up asymmetric" algorithm, as used in Java). :p The choice seems a bit arbitrary, though, since 0.5 is exactly in the middle (and so either way could be considered a correct approach), although the way you suggested is probably more consistent.
Yes, indeed. I was just going by the std::round function, which I'm pretty sure has the behaviour I described (round .5 away from zero).

Quote:

I figured it might be a fairly common homework question so I decided to make it somewhat more obscure (although not obfuscated).
I suppose, though I think that anyone marking code like that (nice indentation, static const variables, default values) would be able to tell from the rest of someone's code whether or not they actually wrote it . . . .

Quote:

>> Multiply by 100.0, round to nearest integer, divide by 100.0

But doesn't that just truncate the value (without rounding)?
Consider 1.234. Multiply it by 100, and you get 123.4. Round this to the nearest integer; you have 123. Divide this by 100 again, and you get 1.23. I think that works, don't you? (At least in theory).

I'm not sure I really understood your question. [/edit]
• 06-12-2009
iMalc
Quote:

Originally Posted by dwks

Consider 1.234. Multiply it by 100, and you get 123.4. Round this to the nearest integer; you have 123. Divide this by 100 again, and you get 1.23. I think that works, don't you? (At least in theory).

I'm not sure I really understood your question. [/edit]