Decimal rounding function

D

Dik T. Winter

> Nick Keighley wrote: ....
>
> Try working out percentages (eg. VAT, interest) using just whole pennies.

If the percentage is a whole number of percents:
amount * percentage / 100.
adjust if that is not the case.
 
B

bartc

Gordon Burditt said:
If all you are transmitting is the binary representation of a
floating point number, there is no way to ensure that the final
string representation (if any) will only use 2 decimals. There are
only 4 patterns that have exact representations in binary floating
point that will naturally use only two or fewer decimal places:

xxxxx.00
xxxxx.25
xxxxx.50
xxxxx.75
and in any case, there's no way to tell that:
2.50000000000000000000000000000000000000000 as a binary floating point
number has one or 20 significant digits to the right of the decimal point,
as 2.5 and 2.500000000000 are represented the same. You need to transmit
the number of significant digits, or something equivalent, along with the
value.

If I want to transmit a number 2.5223987237476 that I know can only really
be the nearest of 2.40, 2.50 and 2.60, I would rather transmit it as
2.500000000000000072 or 2.499999999999999991 (by rounding to 1 decimal) than
as 2.5223987237476.

The tiny deviations from exactly 2.5 are inevitable. Any calculations using
the rounded number will be as accurate as they can be. And printed out in
any number of reasonable places will display as 2.5000000...
decimalround(x,0) can return an exact representation of the rounded
value within a reasonable range. decimalround(x,1) cannot.

There seems to be this preoccupation with this exact representation thing.

If you build a stack of 926 1cm cubes, the height should be 9.26m. That 9.26
of course cannot be represented exactly in binary, but is the best
representation possible.

In the same way, decimalround(9.2614779606,2) might not be exactly 9.26, but
is the best possible.
 
D

Dann Corbit

Greetings to all,

I'm searching for cheap, quick and easy rounding function in C, but (there
had to be but!) without any libraries. In other words, is there a sane
mathematical (or any other, for that matter) way that can be implemented
in C and round arbitrary number of decimal places them to desired decimal
places?

So, basically, something like this:

double round (double number, unsigned decimalPlaces) {
/* some magic here */ }

Basic point is to make it portable for everything from Motorola 68000 to
embedded ARM or latest gaming x64 CPU.

I tried doing it myself but couldn't even begin being puzzled about
determining number of decimal places in the first place, let alone doing
any rounding with them.

Also did some Googling research but found only solutions with strings and
similar tricks (which I was acquainted with) or written in C++. Appreciate
any help or hint.

TIA!

Probably, you really want something like this:
http://www.alphaworks.ibm.com/tech/decnumber

There is a snippet that does the same thing, but I guess it really won't
accomplish what you want to do in the long run.

If you want accurate decimal math, then use a decimal library.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top