round() ?

Hello
How can i round float to 2 digits after '.' ?

Thanx

0
user681 (69)
1/19/2004 3:45:03 PM
vc.mfc 33608 articles. 0 followers. Follow

15 Replies
1395 Views

Similar Articles

[PageSpeed] 21

Quick and Dirty:

Multiply by 100, add 0.5 (to round up correctly). convert to long, then 
back to float, then divide by 100.

This is limited to values that will fit within a long, and is not 
efficient.  It is reliable.

also:

float val;

val = val - fmod(val, .01);

Or, the real thing:

char *_fcvt( double value, int count, int *dec, int *sign );
Which is a char string, which you can then feed to atof() and get your 
rounded value back.  Alas, beware errors in precision limits!


Balboos


user@domain.invalid wrote:

> Hello
> How can i round float to 2 digits after '.' ?
> 
> Thanx
> 

0
balboos (139)
1/19/2004 4:01:30 PM
<user@domain.invalid> wrote in message
news:%232tyyMq3DHA.2144@TK2MSFTNGP10.phx.gbl...
> Hello
> How can i round float to 2 digits after '.' ?
>
> Thanx


I once had to deal with rounding doubles in a financial application (I did
not have fixed precision class), and made following rounding function. You
would better use double to deal with floating point.

#include <cmath>

// Round up to 10 digit precision
double ROUND(double number, int decimals)
{
    static const double EPSILON = 1E-10;
    static const double RNDFACT[] =
        {1E0,1E1,1E2,1E3,1E4,1E5,1E6,1E7,1E8,1E9,1E10};
    static const double bnd_with_epsilon_value_pos = 0.5-EPSILON;
    static const double bnd_with_epsilon_value_neg = 0.5+EPSILON;

    ASSERT(0<=decimals && decimals<=10);
    number *= RNDFACT[decimals];
    double flr  = floor(number); //  2.8 -> 2   -2.8 -> -3
    double rest = number - flr;  //  2.8-2 = 0.8 ; -2.8-(-3) = 0.2
    if(
       (number>=0.0 && rest>=bnd_with_epsilon_value_pos) ||
       (number<0.0 && rest>=bnd_with_epsilon_value_neg)
      )
        number = ceil(number);
    else
        number = flr;
    return number / RNDFACT[decimals];
}

Tom.


0
1/19/2004 4:56:27 PM
Be careful with this one. It might be completely removed by the compiler
optimizations ;-)

Jase

"Balboos" <balboos@masonicbrother.com.No.Spam> wrote in message
news:ubTOb.20152$OM2.4545949@news4.srv.hcvlny.cv.net...
> Quick and Dirty:
>
> Multiply by 100, add 0.5 (to round up correctly). convert to long, then
> back to float, then divide by 100.
>
> This is limited to values that will fit within a long, and is not
> efficient.  It is reliable.
>
> also:
>
> float val;
>
> val = val - fmod(val, .01);
>
> Or, the real thing:
>
> char *_fcvt( double value, int count, int *dec, int *sign );
> Which is a char string, which you can then feed to atof() and get your
> rounded value back.  Alas, beware errors in precision limits!
>
>
> Balboos
>
>
> user@domain.invalid wrote:
>
> > Hello
> > How can i round float to 2 digits after '.' ?
> >
> > Thanx
> >
>


0
jshelley (197)
1/20/2004 4:30:15 AM
 CString cs;
 cs.Format("%.2f", d);
 d = atof(cs);

Normand

<user@domain.invalid> a �crit dans le message de news:
#2tyyMq3DHA.2144@TK2MSFTNGP10.phx.gbl...
> Hello
> How can i round float to 2 digits after '.' ?
>
> Thanx
>


0
NoEmail9920 (149)
1/20/2004 6:06:35 AM
Well whata ya know ;-) I never realised that performed rounding. (yeah...
I've checked it ;-) )

Jase

"Normand" <NoEmail@Please.com> wrote in message
news:Mz3Pb.9552$U77.688636@news20.bellglobal.com...
> CString cs;
>  cs.Format("%.2f", d);
>  d = atof(cs);
>
> Normand
>
> <user@domain.invalid> a �crit dans le message de news:
> #2tyyMq3DHA.2144@TK2MSFTNGP10.phx.gbl...
> > Hello
> > How can i round float to 2 digits after '.' ?
> >
> > Thanx
> >
>
>


0
jshelley (197)
1/20/2004 6:43:34 AM
"Jase" <jshelley@spamblock.enersol.com.au> wrote in message
news:400cce17$0$26114$afc38c87@news.optusnet.com.au...
> Well whata ya know ;-) I never realised that performed rounding. (yeah...
> I've checked it ;-) )

It doesn't round correctly in all cases. You'll see soon enough that for
some numbers, you'll never get the correct rounded number.
I noticed this in a financial application I developed. Formatting does _not_
round correctly in all cases.

You'll be better of using my method, it rounds correctly (at least, I
haven't found a number for which it doesn't round correctly).

Tom.

> Jase
>
> "Normand" <NoEmail@Please.com> wrote in message
> news:Mz3Pb.9552$U77.688636@news20.bellglobal.com...
> > CString cs;
> >  cs.Format("%.2f", d);
> >  d = atof(cs);
> >
> > Normand
> >
> > <user@domain.invalid> a �crit dans le message de news:
> > #2tyyMq3DHA.2144@TK2MSFTNGP10.phx.gbl...
> > > Hello
> > > How can i round float to 2 digits after '.' ?
> > >
> > > Thanx
> > >
> >
> >
>
>


0
1/20/2004 9:50:32 AM
Do you have any details about it,  I mean examples or explanations?
Is it the same with sprintf?

"TT (Tom Tempelaere)" <_N_OSPAMtiti____@hotmail.comMAPSO_N_> a �crit dans le
message de news: IR6Pb.20762$Gf2.3930205@phobos.telenet-ops.be...
> "Jase" <jshelley@spamblock.enersol.com.au> wrote in message
> news:400cce17$0$26114$afc38c87@news.optusnet.com.au...
> > Well whata ya know ;-) I never realised that performed rounding.
(yeah...
> > I've checked it ;-) )
>
> It doesn't round correctly in all cases. You'll see soon enough that for
> some numbers, you'll never get the correct rounded number.
> I noticed this in a financial application I developed. Formatting does
_not_
> round correctly in all cases.
>
> You'll be better of using my method, it rounds correctly (at least, I
> haven't found a number for which it doesn't round correctly).
>
> Tom.
>
> > Jase
> >
> > "Normand" <NoEmail@Please.com> wrote in message
> > news:Mz3Pb.9552$U77.688636@news20.bellglobal.com...
> > > CString cs;
> > >  cs.Format("%.2f", d);
> > >  d = atof(cs);
> > >
> > > Normand
> > >
> > > <user@domain.invalid> a �crit dans le message de news:
> > > #2tyyMq3DHA.2144@TK2MSFTNGP10.phx.gbl...
> > > > Hello
> > > > How can i round float to 2 digits after '.' ?
> > > >
> > > > Thanx
> > > >
> > >
> > >
> >
> >
>
>


0
NoEmail9920 (149)
1/20/2004 3:58:47 PM
"Normand" <NoEmail@Please.com> wrote in message
news:_ecPb.12385$U77.748201@news20.bellglobal.com...
> Do you have any details about it,  I mean examples or explanations?
> Is it the same with sprintf?

There are a lot of numbers that don't round correctly using formatting. I
cannot remember one, it is at least 5 years ago since I had those problems
;-)
But I am very sure that formatting doesn't round correctly in all cases.

And I am sure that my rounding function is 99.9999999...% correct in
rounding any number to 2 digits after the comma. I know this from experience
because since I made and used the rounding function, I had no more rounding
problems. This is a financial application though, and most of the time,
numbers are rounded to 2 or 4 digits after the comma. The rounding function
may prove unstable for eg 10 or more digit rounding, or when rounding
extremely large values.

You (& everybody else) are free to copy and use my rounding function, but
use it wisely.

HTH,
Tom.


> "TT (Tom Tempelaere)" <_N_OSPAMtiti____@hotmail.comMAPSO_N_> a �crit dans
le
> message de news: IR6Pb.20762$Gf2.3930205@phobos.telenet-ops.be...
> > "Jase" <jshelley@spamblock.enersol.com.au> wrote in message
> > news:400cce17$0$26114$afc38c87@news.optusnet.com.au...
> > > Well whata ya know ;-) I never realised that performed rounding.
> (yeah...
> > > I've checked it ;-) )
> >
> > It doesn't round correctly in all cases. You'll see soon enough that for
> > some numbers, you'll never get the correct rounded number.
> > I noticed this in a financial application I developed. Formatting does
> _not_
> > round correctly in all cases.
> >
> > You'll be better of using my method, it rounds correctly (at least, I
> > haven't found a number for which it doesn't round correctly).
> >
> > Tom.
> >
> > > Jase
> > >
> > > "Normand" <NoEmail@Please.com> wrote in message
> > > news:Mz3Pb.9552$U77.688636@news20.bellglobal.com...
> > > > CString cs;
> > > >  cs.Format("%.2f", d);
> > > >  d = atof(cs);
> > > >
> > > > Normand
> > > >
> > > > <user@domain.invalid> a �crit dans le message de news:
> > > > #2tyyMq3DHA.2144@TK2MSFTNGP10.phx.gbl...
> > > > > Hello
> > > > > How can i round float to 2 digits after '.' ?
> > > > >
> > > > > Thanx
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>


0
1/20/2004 5:05:06 PM
On Tue, 20 Jan 2004 10:58:47 -0500, "Normand" <NoEmail@Please.com> wrote:

>Do you have any details about it,  I mean examples or explanations?
>Is it the same with sprintf?

Top posting is evil. Now I can't make comments in line.

The printing functions do not round incorrectly. If I am not mistaken
they round using "round even" rule (available in FPU). In this method,
they round a mid value (.5) up or down to the even number...and yes,
it's usually the same with sprintf().

  2.5 rounds to 2.0 and 3.5 rounds to 4.0

The advantage of this is that rounding errors are statistically
canceled. The average of a sum of rounded numbers will be very 
close to the rounded average of the sum of the unrounded numbers.

The average of {2.5,3.5} is 3.0
The average of {2,4} is 3.0
The average of {3,4} (TT's way} is 3.5!

You can get this automatically, efficiently, and with scaling
from System.Math.Round().

Here is a simpler form of TT's method:

double Round(double number, int decimals){
  double factor=Math.Pow(10,decimals);
  return Math.Floor(number*factor+0.4999999999)/factor;
}

....note that the choice to make digits beyond 1E-10 insignificant is
based on an arbitrary choice.

TT wants ROUND(2.5) = 3.0 and ROUND(-2.5) = -3.0. That's probably ok
for what he's doing but it's not always the right one. It just happens
to work the way he wants it to.

It's inconsistent.
Positive numbers at the mid point are rounded to a larger value, while
negative numbers at a midpoint are rounded to smaller value.

regards,

bullshark

>
>"TT (Tom Tempelaere)" <_N_OSPAMtiti____@hotmail.comMAPSO_N_> a �crit dans le
>message de news: IR6Pb.20762$Gf2.3930205@phobos.telenet-ops.be...
>> "Jase" <jshelley@spamblock.enersol.com.au> wrote in message
>> news:400cce17$0$26114$afc38c87@news.optusnet.com.au...
>> > Well whata ya know ;-) I never realised that performed rounding.
>(yeah...
>> > I've checked it ;-) )
>>
>> It doesn't round correctly in all cases. You'll see soon enough that for
>> some numbers, you'll never get the correct rounded number.
>> I noticed this in a financial application I developed. Formatting does
>_not_
>> round correctly in all cases.
>>
>> You'll be better of using my method, it rounds correctly (at least, I
>> haven't found a number for which it doesn't round correctly).
>>
>> Tom.
>>
>> > Jase
>> >
>> > "Normand" <NoEmail@Please.com> wrote in message
>> > news:Mz3Pb.9552$U77.688636@news20.bellglobal.com...
>> > > CString cs;
>> > >  cs.Format("%.2f", d);
>> > >  d = atof(cs);
>> > >
>> > > Normand
>> > >
>> > > <user@domain.invalid> a �crit dans le message de news:
>> > > #2tyyMq3DHA.2144@TK2MSFTNGP10.phx.gbl...
>> > > > Hello
>> > > > How can i round float to 2 digits after '.' ?
>> > > >
>> > > > Thanx
>> > > >
>> > >
>> > >
>> >
>> >
>>
>>
>

0
bullshark (19)
1/20/2004 7:23:20 PM
On Tue, 20 Jan 2004 19:23:20 GMT, bullshark <bullshark@scubadiving.com> wrote:
>Top posting is evil. Now I can't make comments in line.

For some reason I'll never understand, agent showed me in the C# group
when I made that reply...

The comments still apply, the formula is the same, the reduced method 
is just:

double Round(double number, int decimals){
  double factor=pow(10,decimals);
  return floor(number*factor+0.499999999)/factor;
}

The C-Library doesn't  provide the round even method, but you can do it
anyway, using _controlfp() API call, if you want it.

regards

bullshark

You may now resume regularly scheduled programming.


0
bullshark (19)
1/20/2004 7:40:51 PM
"bullshark" <bullshark@scubadiving.com> wrote in message
news:gvpq00t466j6mgvuon15kdp5prur76q8ai@4ax.com...
> On Tue, 20 Jan 2004 10:58:47 -0500, "Normand" <NoEmail@Please.com> wrote:
>
> >Do you have any details about it,  I mean examples or explanations?
> >Is it the same with sprintf?
>
> Top posting is evil. Now I can't make comments in line.

Trueness.

> The printing functions do not round incorrectly.

From my experience, I can certainly tell that this is not true. I don't
recall the exact number I was having problems with, sorry. But (at least in
MFC/VS6) formatting doesn't round correctly in all cases. I'm not talking
about negative vs positive numbers, I'm talking about numbers that have no
exact representation.

> If I am not mistaken
> they round using "round even" rule (available in FPU). In this method,
> they round a mid value (.5) up or down to the even number...and yes,
> it's usually the same with sprintf().

At first I tried using format, with rounding errors as a result (MFC/VS6).

> You can get this automatically, efficiently, and with scaling
> from System.Math.Round().

That is probably .net code, you are posting in an MFC newsgroup.

> Here is a simpler form of TT's method:
>
> double Round(double number, int decimals){
>   double factor=Math.Pow(10,decimals);
>   return Math.Floor(number*factor+0.4999999999)/factor;
> }

I don't know if this yields the exact same results in all cases. Actually, I
doubt it.

> ...note that the choice to make digits beyond 1E-10 insignificant is
> based on an arbitrary choice.

Indeed, it was an observation in my application that I would never need
digits beyound 10 digits.

> TT wants ROUND(2.5) = 3.0 and ROUND(-2.5) = -3.0. That's probably ok
> for what he's doing but it's not always the right one.

It may not be, but (1) it is mathematically sound and is what is expected -
symmetric, (2) desirable for financial applications;

(1) + (2): client needs to pay 2.515 euro. From the clients perspective he
needs to pay 2.515, or rounded 2.52. From his account 2.52 is subtracted. If
you work with debit & credit terms, you never need negative numbers - no
problems there. If you do not (eg as an optimization), you will have to use
negative numbers when representing debit amounts. In this case
rounding -2.515 rounded should yield -2.52, _not_ -2.51!

> It just happens
> to work the way he wants it to.
> It's inconsistent.
> Positive numbers at the mid point are rounded to a larger value, while
> negative numbers at a midpoint are rounded to smaller value.

That's the whole point. I can't think of _any_ application that would rather
have -2.515 rounded to -2.51.

> regards,
> bullshark

Kind regards,
Tom.

> >
> >"TT (Tom Tempelaere)" <_N_OSPAMtiti____@hotmail.comMAPSO_N_> a �crit dans
le
> >message de news: IR6Pb.20762$Gf2.3930205@phobos.telenet-ops.be...
> >> "Jase" <jshelley@spamblock.enersol.com.au> wrote in message
> >> news:400cce17$0$26114$afc38c87@news.optusnet.com.au...
> >> > Well whata ya know ;-) I never realised that performed rounding.
> >(yeah...
> >> > I've checked it ;-) )
> >>
> >> It doesn't round correctly in all cases. You'll see soon enough that
for
> >> some numbers, you'll never get the correct rounded number.
> >> I noticed this in a financial application I developed. Formatting does
> >_not_
> >> round correctly in all cases.
> >>
> >> You'll be better of using my method, it rounds correctly (at least, I
> >> haven't found a number for which it doesn't round correctly).
> >>
> >> Tom.
> >>
> >> > Jase
> >> >
> >> > "Normand" <NoEmail@Please.com> wrote in message
> >> > news:Mz3Pb.9552$U77.688636@news20.bellglobal.com...
> >> > > CString cs;
> >> > >  cs.Format("%.2f", d);
> >> > >  d = atof(cs);
> >> > >
> >> > > Normand
> >> > >
> >> > > <user@domain.invalid> a �crit dans le message de news:
> >> > > #2tyyMq3DHA.2144@TK2MSFTNGP10.phx.gbl...
> >> > > > Hello
> >> > > > How can i round float to 2 digits after '.' ?
> >> > > >
> >> > > > Thanx
> >> > > >
> >> > >
> >> > >
> >> >
> >> >
> >>
> >>
> >
>


0
1/20/2004 11:27:58 PM
Back in the good old days, we disabled the optimizer whenever the 
mathematical needs were critical (had to be done right).  What is 
needed, of course, is the following:

#pragma KEEP_YOUR_GRUBBY_OPTIMIZING_PAWS_OFF_THIS_BLOCK
.... clever mathematical code ...
#pragma OK_GO_AHEAD_AND_AMUSE_YOURSELF

"When I use a word, it means precisely what I wish it to mean - neither 
more nor less"  Humpty Dumpty (Through the Looking Glass)

Balboos

P.S.  Thanks, again, to the group for the help with the Property Sheets

Jase wrote:
> Be careful with this one. It might be completely removed by the compiler
> optimizations ;-)
> 
> Jase
> 
> "Balboos" <balboos@masonicbrother.com.No.Spam> wrote in message
> news:ubTOb.20152$OM2.4545949@news4.srv.hcvlny.cv.net...
> 
>>Quick and Dirty:
>>
>>Multiply by 100, add 0.5 (to round up correctly). convert to long, then
>>back to float, then divide by 100.
>>
>>This is limited to values that will fit within a long, and is not
>>efficient.  It is reliable.
>>
>>also:
>>
>>float val;
>>
>>val = val - fmod(val, .01);
>>
>>Or, the real thing:
>>
>>char *_fcvt( double value, int count, int *dec, int *sign );
>>Which is a char string, which you can then feed to atof() and get your
>>rounded value back.  Alas, beware errors in precision limits!
>>
>>
>>Balboos
>>
>>
>>user@domain.invalid wrote:
>>
>>
>>>Hello
>>>How can i round float to 2 digits after '.' ?
>>>
>>>Thanx
>>>
>>
> 
> 

0
balboos (139)
1/21/2004 12:05:21 AM
Back in the good old days, we disabled the optimizer whenever the 
mathematical needs were critical (had to be done right).  What is 
needed, of course, is the following:

#pragma KEEP_YOUR_GRUBBY_OPTIMIZING_PAWS_OFF_THIS_BLOCK
.... unusually clever mathematical code ...
#pragma OK_GO_AHEAD_AND_AMUSE_YOURSELF

"When I use a word, it means precisely what I wish it to mean - neither 
more nor less"  Humpty Dumpty (Through the Looking Glass)

Balboos

P.S.  Thanks, again, to the group for the help with the Property Sheets


Jase wrote:

> Be careful with this one. It might be completely removed by the compiler
> optimizations ;-)
> 
> Jase
> 
> "Balboos" <balboos@masonicbrother.com.No.Spam> wrote in message
> news:ubTOb.20152$OM2.4545949@news4.srv.hcvlny.cv.net...
> 
>>Quick and Dirty:
>>
>>Multiply by 100, add 0.5 (to round up correctly). convert to long, then
>>back to float, then divide by 100.
>>
>>This is limited to values that will fit within a long, and is not
>>efficient.  It is reliable.
>>
>>also:
>>
>>float val;
>>
>>val = val - fmod(val, .01);
>>
>>Or, the real thing:
>>
>>char *_fcvt( double value, int count, int *dec, int *sign );
>>Which is a char string, which you can then feed to atof() and get your
>>rounded value back.  Alas, beware errors in precision limits!
>>
>>
>>Balboos
>>
>>
>>user@domain.invalid wrote:
>>
>>
>>>Hello
>>>How can i round float to 2 digits after '.' ?
>>>
>>>Thanx
>>>
>>
> 
> 

0
balboos (139)
1/21/2004 12:12:17 AM
> double Round(double number, int decimals){
>   double factor=pow(10,decimals);
>   return floor(number*factor+0.499999999)/factor;
> }
> 
> The C-Library doesn't  provide the round even method, but you can do it
> anyway, using _controlfp() API call, if you want it.

This is my roundung rountine. I never noticed any problems with it.
Any comments?

double round(const double& inValue)
{
	double xShift = inValue - .5;
	double nShift = fix(xShift);
	if (xShift == nShift)
	{
		if (nShift == 2.*fix(nShift/2.))
			xShift -= .25;		// nShift even, round off
		else
			xShift += .25;		// nShift odd, round up
	}
	return ceil(xShift);
}
0
1/21/2004 10:26:26 AM
On Tue, 20 Jan 2004 23:27:58 GMT, "TT \(Tom Tempelaere\)" <_N_OSPAMtiti____@hotmail.comMAPSO_N_> wrote:

>"bullshark" <bullshark@scubadiving.com> wrote in message
>> If I am not mistaken
>> they round using "round even" rule (available in FPU). In this method,
>> they round a mid value (.5) up or down to the even number...and yes,
>> it's usually the same with sprintf().
>
>At first I tried using format, with rounding errors as a result (MFC/VS6).

The round even rule is an ieee standard 754 section 4...not an error.

>
>I don't know if this yields the exact same results in all cases. Actually, I
>doubt it.

big ooops there. I didn't think hard enough about effects of epsilon 
and FPU inexactness...I did try it, but happened to pick happy numbers.

Would this do the same?

   int signed=number<0;
   double factor=pow(10.0,decimals);
   number=floor(fabs(number)*factor+0.5)/factor;
   return signed?-number:number;

....even with the pow call it's almost as fast (14% rounded (c:),
but in either case insignificant. With the addition of static factors
its the same speed (at least over 18Million reps). There's nothing wrong 
with your code, it's just a little arcane and easily broken if the ASSERT 
is off. It also can't do something like: 

   round(256751335.12345,-3) (nearest 1000 = 256751000.000000


>It may not be, but (1) it is mathematically sound and is what is expected -
>symmetric, (2) desirable for financial applications;

It may be desirable for financial applications, but floating point is not.
As to "mathematically sound", a matter of opinion. Reconsider the averages example.

>
>(1) + (2): client needs to pay 2.515 euro. From the clients perspective he
>needs to pay 2.515, or rounded 2.52. From his account 2.52 is subtracted. If
>you work with debit & credit terms, you never need negative numbers - no
>problems there. If you do not (eg as an optimization), you will have to use
>negative numbers when representing debit amounts. In this case
>rounding -2.515 rounded should yield -2.52, _not_ -2.51!

1) the round even rule would round 2.515 to 2.52
2) a bank that always rounds mid values away from zero stands to
   gain a half on some transactions, or lose a half point on others.
   Transaction flows in high volumes can cause large overages/shortfalls
   and unaccountable discrepancies. Round even rule makes all the numbers
   add up all the time to within 1 occurrence of rounding tolerance.
3) negative numbers get the same treatment as positive. 
   i.e. abs(roundEven(-num))==roundEven(num), but as you point out,
   sensible financial apps do not use signed numbers, so the sign issue
   should be immaterial.

>That's the whole point. I can't think of _any_ application that would rather
>have -2.515 rounded to -2.51.

You haven't worked with statistics programs, or scientific applications?
The point of round even is that it doesn't skew data or make it dirty.
Errors introduced by rounding with round even are self-cancelling. In 
scientific calculations it is generally considered mandatory to follow 
the round even rule. Otherwise computations based on printed materials
( necessarily rounded ) hardly work out.

BTW, which way to round mid-value numbers in financial applications is
a matter of accounting policy and occasionally law, but not one of fact.

e.g. your case (euros) http://europa.eu.int/euro/quest/pdf/quest4en.pdf

e.g. NIST rounding rules for SI Units (a very explicit round-even)
http://www.physics.nist.gov/Pubs/SP811/appenB.html
....can be found in section B-7.

Round-even is supported by the Intel FPU (round near), so it is very efficient 
when used, and it's widespread demand obvious by it's inclusion.
In fact, it's the only 'round' (as we think of it) supported. Everything
else is chop/floor/ceil. Your application uses it by default as the
FPU converts in and out of 80-bit(internal) format to double and float.

I would say the sprintf libs don't round incorrectly, they just don't 
round the way you expected. 

The roundEven can be had on WinIntel with:

double ieeeRound(double num){
    /* MS defaults fpu control word to round near (_RC_NEAR)*/
    __asm{     //don't know how to do this in C
       FLD num
       FRNDINT
    }
}


Variability of round behavior is probably the reason StdC libs don't
provide a round method.

regards,

bullshark
0
bullshark (19)
1/21/2004 6:14:45 PM
Reply:

Similar Artilces:

round() ?
Hello How can i round float to 2 digits after '.' ? Thanx Quick and Dirty: Multiply by 100, add 0.5 (to round up correctly). convert to long, then back to float, then divide by 100. This is limited to values that will fit within a long, and is not efficient. It is reliable. also: float val; val = val - fmod(val, .01); Or, the real thing: char *_fcvt( double value, int count, int *dec, int *sign ); Which is a char string, which you can then feed to atof() and get your rounded value back. Alas, beware errors in precision limits! Balboos user@domain.invalid wrote: >...

rounding #9
is there a way to have a column round automatically instead of having to round onto another column? I don't want to add another column to just be able to round. -- denhar ------------------------------------------------------------------------ denhar's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=24089 View this thread: http://www.excelforum.com/showthread.php?threadid=378356 If the column contains formulas, the formulas can incorporate the Round() function within themselves. For example, if the formula is: =A1*B1 it could be revised to: =ROUND(A1*...

Rounding question
I'm using Excel 2007 but 'save as' 97-2003 as colleagues have different versions. All number formats are set to 2 decimal places. I'm finding that percentage calculations are rounding up to 2 decimal places but when the result in currency is subtracted from another figure the sum is rounded down. This gives results such as 6 - 3 = 4. I'm sure there must be a simple answer but I can't find it. Hoping someone here knows, as checking all simple calcuations is getting ridiculous ! Carrie "Carrie" <pantscarolyn.smith2@ntlworld.com> w...

Problem with rounding currency values
Hi, I am using the Sales Global Procedure SOP_Calculate_Trade_Discount_Split. This procedure returns the line trade discount. The line trade discount returned is rounded off. eg: if value is 6.76 then it returns 7.00. Is there some place where i can specify to turn off the rounding off? The output type is Currency. Any help is greatly appreciated. Thanks & Regards, Sup ...

Round Up and Round Down Time
Good Morning & a Happy New Year to all subscribers in this group. I want to round up Start times and round down Finish times to the next or previous 15 minutes in my spreadsheet. I have found MROUND but this does not work for me in all cases. There does not seem to be MROUNDUP or MROUNDDOWN available. I can get the results I want using a VLOOKUP table but is there another way? Regards to all, Dave Moore =FLOOR(A1,TIME(,15,)) =CEILING(A1,TIME(,15,)) -- David Biddulph DaveMoore wrote: > Good Morning & a Happy New Year to all subscribers in this group. > >...

Automatic Rounding problems
I am using Money 99 and, only on investments section, when I try enter a price or quantity with a decimal part, it simply round it to an integer number automatically when I exit the edit box. Do you know how can I make it work correctly. My Contorl Panel / Currency is already configured to 2 decimal digits and it works perfectly in the other fields where I have to put a value. Thanks for your help. Fabio Wasn't Money 99 around before they decimalized stocks? May not be possible to fix it... Fabio Pires wrote: > I am using Money 99 and, only on investments section, > when...

Rounding numbers in charts
HHi U'm not very good with computers. And I did a spreadsheet on excel and had percentages I wanted to put in a chart...when I created the chart (piechart) the values came out all inacurate. I wanted one to be 8% and it came out 6%. I heard you couold try and fix it using decimal places but that only makes a minor difference. Is there something I dont know about thats fairly simple to do? Hi Andrea If you'ld like to give us an example of your data (e.g. three categories and the associated percentages) and explain to us where the percentages came from - did you type th...

How to round & sum only the displayed (rounded) numbers
I have rounded some numbers to the thousands digits. I need help finding a formula that rounds the displayed (rounded thousands) digits. Everything I try only calculates it based on the unrounded numbers and as such many sum numbers are off by 1 (or really 1,000). Any help would be awesome! thanks On Fri, 19 Feb 2010 06:57:01 -0800, ALG <ALG@discussions.microsoft.com> wrote: >I have rounded some numbers to the thousands digits. I need help finding a >formula that rounds the displayed (rounded thousands) digits. Everything I >try only calculates it based on t...

Time, & Round?
My spread sheet is used to track arrival times at a specific location. The location has a scheduled arrival time in column C, in column D I enter the actual arrival time when a vehicle arrives, and column E calculates the difference. I use the 1904 time system so I’ll be able to calculate and show negative time. Column E is set with =IF(D3=””,””,D3-C3) this will have column E appear blank until the actual time is entered. Without =IF(D3=””,””,D3-C3)in column E and with =D3-C3 this will show the negative of the scheduled time i.e. -4:59 or -18:44. The schedule time is always pr...

Formula for rounding numbers.
I would like the formula for the following situation. How do I make all numbers in cell A3 from 1-32 round up to 33, all numbers 34-65 round up to 66 and all numbers 67-99 round up to 100? Thank you for any help. Alexis Hi Alexis See if =INT(CEILING(A3,100/3)) does what you want. HTH. Best wishes Harald "Alexis Anthony" <alexisanthony1@yahoo.com> skrev i melding news:56c40e33.0405021336.1aa773f0@posting.google.com... > I would like the formula for the following situation. > > How do I make all numbers in cell A3 from 1-32 round up to 33, all > numbers 34-65 r...

Rounding to
I have a column of numbers, all multiples of 10, starting at 100 and going up into the thousands. I want to round them to the nearest 50, so 140 would become 150 and so would 160. How do I do that? TIA Steven I plan to live forever.....or die trying! =ROUND(A1/50,0)*50 -- HTH Bob Phillips (replace xxxx in the email address with gmail if mailing direct) "Steven" <none@myisp.com> wrote in message news:#2XwkuAEHHA.1224@TK2MSFTNGP04.phx.gbl... > I have a column of numbers, all multiples of 10, starting at 100 and going > up into the thousands. I want...

Adding round bitmap
Hi, everybody: Is it possible to add bitmap picture to my dialog which is not a rectangle? If I do it the simple way - add picture control to my dialog (which is rectangle) and then assign to it some bitmap, which is not rectangle, dark background can be seen in the places where bitmap doesn't fill in picture control. Is it possible to avoid it? Thanks, Alex I would use CDC::SelectClipRgn() to set the region of the DC to the shape you want, rounded rect in your case, and then do the bitblt. AliR. "Alex" <alsim123@hotmail.com> wrote in message news:1177602846.2566...

Rounding Up
Is it possible to perform a calculation and have the result rounded up to the next whole number? I don't ever want it rounded down. Hi =ROUNDUP(your_formula,0) -- Regards Frank Kabel Frankfurt, Germany Keith wrote: > Is it possible to perform a calculation and have the result rounded > up to the next whole number? I don't ever want it rounded down. ...

Rounding Currency
Hello MVP's Is there a way that I can round this formula that I have for a control source of a textbox txtSubTotal value = 3.15 Textbox1 ControlSource =Nz([txtSubTotal]*0.0675,0) So instead of this $0.2126 I would get this $0.21 The Textbox that I have the formula in looks like $0.21 but I have a command button that adds this the Textbox value to a subform which turns the 0.21 to 0.2126 There is a ROUND function which allows you to specify to how many decimal places you want to round. -- Bob Larson Access World Forums Super Moderator ____________________________________ If my post was...

Rounding seconds
Hello All, I need to calculate times to the second (hh:mm:ss) but display the results to the nearest minute. However using the the hh:mm format simply removes the seconds - how can I get Excel to round to the nearest minute? Thanks in advance, K ------------------------------------------------ ~~ Message posted from http://www.ExcelTip.com/ ~~ View and post usenet messages directly from http://www.ExcelForum.com/ Kenneth, You can't use formatting to round the time value. You will either have to live with the truncation provided by the hh:mm format, or actually round the value in t...

Round
I'm making price list. 150 articles and I want that every price have last number 9. Ex. 1651,00 = 1659,00 1255,00 = 1259,00 etc... Help! =3DINT(A1/10)*10+9 HIH On 26 Mar, 09:49, Vladek <Vla...@discussions.microsoft.com> wrote: > I'm making price list. 150 articles and I want that every price have last > number 9. > Ex. 1651,00 =3D 1659,00 > =A0 =A0 =A01255,00 =3D 1259,00 > etc... > > Help! Hi, Try this =CEILING(A1,10)-1 -- Mike When competing hypotheses are otherwise equal, adopt the hypothesis that introduces th...

How can I turn off rounding for numbers???
Excel 2003: I've got a column of cells. I highlight the entire column, do a format / cells / number then I choose 0 decimal places I have a list of serial numbers that I want to input, and then "fill down"... because they're all in sequence. However, when I input. They are 14 digits long. Every time I input a 14-digit number, Excel forces it to round up to the 5th number from the right. How can I get it to stop doing this? Hi Excel should not round in this case. Maybe it shows the number in the scientific notation. Try the folowing custom format: 00000000000000 >...

round
Here is what I am trying to do I have 2 columns that have been formated and formula added to calculate the difference between 2 times. e.g 22:00-01:00 equals 3 hours. The problem I am having is when the times are 22:00 to midnight or 00:00. It will not show anything. So I have to input either 23:59 or 00:01, the problem with this is that the resulting time shown is either 2.98 or something similar How do I get it to round to the nearest quarter hour Bobby "bobby todd" <anonymous@discussions.microsoft.com> wrote in message news:A47EF5F0-ABC7-405B-B3E8-86F24CD5DBBA@microsoft....

Rounding Errors Help
Is there any way to display a rounded number in a cell, but use th original unrounded number when other cells reference that cell in calculation? It would be a pain to use the components of the rounde cell in the calculation in which i would need the unrounded number. I need a function that displays a rounded number, but uses th unrounded number in calculations -- mattflo ----------------------------------------------------------------------- mattflow's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=2512 View this thread: http://www.excelforum.com/showthread.ph...

Issues with Rounding (or lack thereof)
Hello, I've got a question with an Access 2000 Database. I've got a query with a calculated field: Der_Percent_Good. Der_Percent_Good: Nz((100*(1-[Num_Out_Tol]/[Shipment_Sample_Size])),"N/ A") This field is supposed to be a percentage, and I'd like this field to be able to round to 2 decimal places, for right now it does not. Right now it has a large amount of decimal places if the situation arises, like 1/3 (goes out of view on my textbox on the form), and it will have 0 decimal places if it turns out to be a whole number (0/100). I've tried selecting Properties-...

Fixing/ Rounding
Hello, I want to calculate how many rows I need in a grid for a given number of cells when the number of columns is limited to let's say 6. Somehow I don't find any better way than using Mod because lCellCount=1917 lMaxCols = 6 would return 319,5 but VB6 rounds that down to 319 instead of rounding it up. This means I get 1 row less than I really need. I was only able to help me by using Mod but that looks fishy/ unncessary to me. Here is my code: Dim lRows& lRows = lCellCount \ lMaxCols Dim iMod% iMod = lCellCount Mod lMaxCols ...

Rounding up price lists
Can you round to the nearest Dollar? Yes, if the Price Method is one that caculates the selling price (such as % of List, markup from list, etc). Click on Options, select the Round Option and set to multiple of and enter 1.00 in the Round Amount field. -- Richard L. Whaley Author / Consultant / MVP Documentation for Software Users Get our Free Tips and Tricks Newsletter and check out our books at http://www.AccoladePublications.com "tstrop" wrote: > Can you round to the nearest Dollar? ...

Rounding Off
Hi All I was just wondering if there was a way of Excel showing a numbe without rounding it off? When I enter a number like 7.8, it rounds it off to 8.0, and i was jus wondering if there was a way to stop this from happening as i woul prefer the 7.8 to be shown. Thanks in advance Mar -- Message posted from http://www.ExcelForum.com Hi just format the cell with some decimla points (goto 'format - Cells') -- Regards Frank Kabel Frankfurt, Germany "sparky3883 >" <<sparky3883.14e396@excelforum-nospam.com> schrieb im Newsbeitrag news:sparky3883.14e396@excelfor...

Rounding up #4
Is there a way to round up a number as soon as it becomes higher than the whole number? I am writing a painting estimating file, and would like for the number to always round up, so for example, if I need 10.01 gallons of paint, it becomes 11 gallons of paint. Thanks Gary Do you mean in the cell the number is entered in? Or in another formula = that references that cell (for example, the cell where you multiply the = gallons by the price per gallon)? If you mean the same cell, would = require VB coding (which would physically change the cell value from = what you typed to the rounded...

Rounding Up or Down
Version: 2008 Operating System: Mac OS X 10.6 (Snow Leopard) Processor: Intel How do I round up or down to the nearest $ .25 increment in a formula written in to express currency. <br><br>Thanks, <br> Todd In article <59bb28d6.-1@webcrossing.JaKIaxP2ac0>, ToddM@officeformac.com wrote: > Version: 2008 > Operating System: Mac OS X 10.6 (Snow Leopard) > Processor: Intel > > How do I round up or down to the nearest $ .25 increment in a formula written > in to express currency. <br><br>Thanks, <br> > Todd Read ...