Re: Rounding double
On Nov 21, 9:39 pm, md <mojtaba.da...@ gmail.comwrote:
A double probably doesn't have a decimal point. Floating point values
are commonly represented in binary, not decimal. So what you are
asking for is generally impossible.
The real number which this function is intended to compute might not
be representable in the floating point format used by your double
type.
At best, you can only write this function such that it produces a
close approximation of that number. But still, this function is
silly, and serves no purpose.
Usually this type of rounding is done in two situations.
One situation is that you are doing some kind of scientific or
mathematic computing, and want to display results to a given decimal
precision. In that case, the rounding and truncation is handed in the
conversion of floating point values to text in the output routine. You
do not actually massage your data to do the rounding. The number-to-
string routine you use, whether it be within of printf or C++ ostreams
or whatever, will do the job of rendering a printed representation of
the number in decimal to the specified precision. You never adjust the
internal representation to achieve this. Internally, you always keep
the maximum precision afforded to you by the machine. A roudning
function like the above is of little use to you.
The second situation is that you are doing financial computing, and
need internally to have exact decimal-based arithmetic that follows
certain prescibed rounding rules. All intermediate results in
financial calculations must obey these rules. Whatever is printed in a
financial statement matches the internal representation. If your bank
book says that an account had 1234.53 dollars after a certain
transaction, it means exactly that. It doesn't mean there were
actually 1234.5321 dollars, which were printed to the nearest cent. In
this situation, it is simply inappropriate to be using floating-point
numbers, and so a routine which simulates decimal rounding over the
double type is also of no use.
On Nov 21, 9:39 pm, md <mojtaba.da...@ gmail.comwrote:
Hi
>
Does any body know, how to round a double value with a specific number
of digits after the decimal points?
>
Does any body know, how to round a double value with a specific number
of digits after the decimal points?
are commonly represented in binary, not decimal. So what you are
asking for is generally impossible.
A function like this:
>
RoundMyDouble (double &value, short numberOfPrecisi ons)
>
It then updates the value with numberOfPrecisi ons after the decimal
point.
>
RoundMyDouble (double &value, short numberOfPrecisi ons)
>
It then updates the value with numberOfPrecisi ons after the decimal
point.
be representable in the floating point format used by your double
type.
At best, you can only write this function such that it produces a
close approximation of that number. But still, this function is
silly, and serves no purpose.
Usually this type of rounding is done in two situations.
One situation is that you are doing some kind of scientific or
mathematic computing, and want to display results to a given decimal
precision. In that case, the rounding and truncation is handed in the
conversion of floating point values to text in the output routine. You
do not actually massage your data to do the rounding. The number-to-
string routine you use, whether it be within of printf or C++ ostreams
or whatever, will do the job of rendering a printed representation of
the number in decimal to the specified precision. You never adjust the
internal representation to achieve this. Internally, you always keep
the maximum precision afforded to you by the machine. A roudning
function like the above is of little use to you.
The second situation is that you are doing financial computing, and
need internally to have exact decimal-based arithmetic that follows
certain prescibed rounding rules. All intermediate results in
financial calculations must obey these rules. Whatever is printed in a
financial statement matches the internal representation. If your bank
book says that an account had 1234.53 dollars after a certain
transaction, it means exactly that. It doesn't mean there were
actually 1234.5321 dollars, which were printed to the nearest cent. In
this situation, it is simply inappropriate to be using floating-point
numbers, and so a routine which simulates decimal rounding over the
double type is also of no use.
Comment