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

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

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

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

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