_snprintf

Discussion in 'C++' started by OMC, Jul 21, 2005.

  1. OMC

    OMC Guest

    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
    OMC, Jul 21, 2005
    #1
    1. Advertising

  2. In message <OcRDe.5527$>, OMC
    <> 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
    Richard Herring, Jul 22, 2005
    #2
    1. Advertising

  3. OMC

    OMC Guest

    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:...
    > In message <OcRDe.5527$>, OMC
    > <> 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
    OMC, Jul 22, 2005
    #3
  4. OMC

    Kevin Handy Guest

    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 =----
    Kevin Handy, Jul 22, 2005
    #4
  5. OMC

    Ben Pope Guest

    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...
    Ben Pope, Jul 23, 2005
    #5
  6. In message <Mr8Ee.9529$>, OMC
    <> top-posted

    [top-posting corrected]

    >
    >"Richard Herring" <junk@[127.0.0.1]> wrote in message
    >news:...
    >> In message <OcRDe.5527$>, OMC
    >> <> 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
    Richard Herring, Jul 25, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page