_snprintf

O

OMC

does anyone know how to stop the rounding when trying to format a decimal?

if I have a value of 9.9999
and I want to format this with a maximum of 3 decimal places, when I use the
"%4.3lf" format specification, I find the number gets rounded up to 10.

Is there any way to get 9.999 ?

Thanks in advance,
Dave
 
R

Richard Herring

OMC said:
does anyone know how to stop the rounding when trying to format a decimal?

if I have a value of 9.9999
and I want to format this with a maximum of 3 decimal places, when I use the
"%4.3lf" format specification, I find the number gets rounded up to 10.

Which is correct behaviour, of course. 9.9999 is much closer to 10.0000
than it is to 9.9990.
Is there any way to get 9.999 ?

Why do you want to display deliberately incorrect results?
 
O

OMC

I will grant you that this is an edge case, but we have some strict
formatting rules in the software and if a result is formatted to be of the
form x.xxx, then we disallow any number that does not conform to that.

so in this case, 10.00 would be an illegal result, so 9.999 is preferable.

and my problem is I cannot figure out how to stop the rounding.
 
K

Kevin Handy

OMC said:
I will grant you that this is an edge case, but we have some strict
formatting rules in the software and if a result is formatted to be of the
form x.xxx, then we disallow any number that does not conform to that.

so in this case, 10.00 would be an illegal result, so 9.999 is preferable.

and my problem is I cannot figure out how to stop the rounding.

// Arbitrary formatting rules

if (x>9.999)
x=9.999;

snprintf..., x ....
 
B

Ben Pope

OMC said:
does anyone know how to stop the rounding when trying to format a decimal?

if I have a value of 9.9999
and I want to format this with a maximum of 3 decimal places, when I use the
"%4.3lf" format specification, I find the number gets rounded up to 10.

Is there any way to get 9.999 ?

subtract 0.0005 first?

Thats a hack, of course... it won't play nice with negative numbers for a start, so:
x<0 ? x+0.0005 : x-0.0005;

Perhaps you can make use of some floor function?

Ben
 
R

Richard Herring

In message <[email protected]>, OMC
<[email protected]> top-posted

[top-posting corrected]
I will grant you that this is an edge case, but we have some strict
formatting rules in the software and if a result is formatted to be of the
form x.xxx, then we disallow any number that does not conform to that.

so in this case, 10.00 would be an illegal result, so 9.999 is preferable.

and my problem is I cannot figure out how to stop the rounding.

Well, you could take the Fortran approach:

if (x>0.9995)
printf("*.***");
else
// as before...

At least your code won't be lying to the user.

[quoted signature deleted]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top