Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > _snprintf

Reply
Thread Tools

_snprintf

 
 
OMC
Guest
Posts: n/a
 
      07-21-2005
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


 
Reply With Quote
 
 
 
 
Richard Herring
Guest
Posts: n/a
 
      07-22-2005
In message <OcRDe.5527$(E-Mail Removed)>, OMC
<(E-Mail Removed)> writes
>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?
>


--
Richard Herring
 
Reply With Quote
 
 
 
 
OMC
Guest
Posts: n/a
 
      07-22-2005
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.

"Richard Herring" <junk@[127.0.0.1]> wrote in message
news:(E-Mail Removed)...
> In message <OcRDe.5527$(E-Mail Removed)>, OMC
> <(E-Mail Removed)> writes
>>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?
>>

>
> --
> Richard Herring



 
Reply With Quote
 
Kevin Handy
Guest
Posts: n/a
 
      07-22-2005
OMC wrote:
> 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 ....

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
Ben Pope
Guest
Posts: n/a
 
      07-23-2005
OMC wrote:
> 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
--
I'm not just a number. To many, I'm known as a String...
 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      07-25-2005
In message <Mr8Ee.9529$(E-Mail Removed)>, OMC
<(E-Mail Removed)> top-posted

[top-posting corrected]

>
>"Richard Herring" <junk@[127.0.0.1]> wrote in message
>news:(E-Mail Removed)...
>> In message <OcRDe.5527$(E-Mail Removed)>, OMC
>> <(E-Mail Removed)> writes
>>>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?
>>>

>>

>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]

--
Richard Herring
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments