Home

On Sep 5, 5:22 am, wombat <6...@k.com> wrote:
> Is there a way to prevent rounding when using printf for print a
> double/float?

> Example:
> double x=3.51;
> printf("%.0f\n", x);

> The output of this is 4, when I want it to be 3.

> I've been surfing the net but I've been coming up empty. Big thanks for
> any help.

Nothing to do with printf: the same rules apply with the cleaner
iostreams. You can use floor() or ciel() to control how you
want rounded; C99 (and the next version of C++) add some more
(round() and trunc(), for example), which may already be
available with your compiler(s).

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

previous
next

Can You Program?
Re: How many lines of code per day should a good programmer produce?
Re: comma overload semantics
Re: Is this C style function well written and thread safe?
Re: datetime in microseconds
Niechciane i Zapomniane
Podaruj Zycie
Nasze Dzieci
Dzieci Niczyje
Fundacja Hobbit