G
G
German2018-12-04 19:35:37
C++ / C#
German, 2018-12-04 19:35:37

How to display a real number, discarding extra zeros in the part after the decimal point?

For example, there is a number 1.234, if we display it through cout, then we will lose the fractional part, the number is rounded off.
And to use fixed setprecision (), we need to know how many unnecessary zeros are left in the fractional part, that is, it does not fit. We work with the double type.
do not offer printf, because I do not know what precision the number will have and how many such zeros are in the fractional part.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
d_ilyich, 2018-12-05
@d_ilyich

Alternatively, use a function like sprintf that "prints" to a buffer, and then manually remove the extra zeros.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question