I'm using fscanf to input floats from a file, how do I specify the number of decimal places I want read? The inputs are things like "123.456" and not "123.4560000" (or, as sometimes happens, "123.459999".
I'm using fscanf to input floats from a file, how do I specify the number of decimal places I want read? The inputs are things like "123.456" and not "123.4560000" (or, as sometimes happens, "123.459999".
You could round the number after it has been read. I don't think scanf() supports printf()'s %.2f modifier.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Ahh, OK. How do I round, then? The round functions I've found round it to the nearest integer. Is there an easier way than multiplying by 10^x first?
Not really. Multiplying it out isn't really that difficult.
You can add .5 and cast to int rather than using the C99 round function:
Code:double nearest_percent(double n) { return (int)(n * 100 + .5) / 100.0; }
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Got it, thanks!