454,904 Members | 4,621 Online
Need help? Post your question and get tips & solutions from a community of 454,904 IT Pros & Developers. It's quick & easy.

# _snprintf

 P: n/a 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 Jul 23 '05 #1
5 Replies

 P: n/a In message , OMC writesdoes anyone know how to stop the rounding when trying to format a decimal?if I have a value of 9.9999and 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 Jul 23 '05 #2

 P: n/a 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" wrote in message news:TV**************@baesystems.com... In message , OMC writesdoes anyone know how to stop the rounding when trying to format a decimal?if I have a value of 9.9999and I want to format this with a maximum of 3 decimal places, when I usethe"%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 Jul 23 '05 #3

 P: n/a 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 =---- Jul 23 '05 #4

 P: n/a 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... Jul 23 '05 #5

 P: n/a In message , OMC top-posted [top-posting corrected] "Richard Herring" wrote in messagenews:TV**************@baesystems.com... In message , OMC writesdoes anyone know how to stop the rounding when trying to format a decimal?if I have a value of 9.9999and I want to format this with a maximum of 3 decimal places, when I usethe"%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 strictformatting rules in the software and if a result is formatted to be of theform 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 Jul 25 '05 #6

### This discussion thread is closed

Replies have been disabled for this discussion.