|
From: Charles Tam on 26 Mar 2006 17:59 I would like to round off a float value to one decimal place. How do I implement this correctly, so that, it would handle rounding up/down as required. For example, 5.41 becomes 5.4, and 5.45 becomes 5.5
From: Eugenio MirĂ³ on 26 Mar 2006 18:29 /* Hi Charles Here you have a sample to se how: you have two methods expressed here, a function that rounds the numbers as you are looking for to use in calculations, and a printf way to do it easely just for output: */ double round(double number, int decimalPlaces) { double dResult = number; int nMultiplier = 1; for (int i = 0; i < decimalPlaces; i++) nMultiplier *= 10; return (double)(int)(dResult * nMultiplier + .5) / nMultiplier; } int main(int argc, char* argv[]) { printf("%f, %f", round(5.41, 1), round(5.45, 1)); printf("%.1f, %.1f", 5.41, 5.45); return 0; } "Charles Tam" wrote: > I would like to round off a float value to one decimal place. > > How do I implement this correctly, so that, it would handle rounding up/down > as required. > > For example, 5.41 becomes 5.4, and > 5.45 becomes 5.5 >
From: Tim Roberts on 27 Mar 2006 03:35 Charles Tam <CharlesTam(a)discussions.microsoft.com> wrote: > >I would like to round off a float value to one decimal place. > >How do I implement this correctly, so that, it would handle rounding up/down >as required. > >For example, 5.41 becomes 5.4, and >5.45 becomes 5.5 I just want to point out that you are treading in a dangerous area. In almost every case, you want to keep the number with as much precision as you can right up until the time you display it. It is quite normal to round floats for humans to read them. Remember that the number 5.4 cannot be exactly represented in binary (although 5.5 can). Thus, 5.4 is just as much of an approximation as 5.41. Both of them are displayed with %6.1f as "5.4", but neither one is exact. -- - Tim Roberts, timr(a)probo.com Providenza & Boekelheide, Inc.
From: Charles Tam on 27 Mar 2006 16:20 Thanks for your reply. "Eugenio MirĂ³" wrote: > /* > Hi Charles > > Here you have a sample to se how: > you have two methods expressed here, a function that rounds the numbers as > you are looking for to use in calculations, and a printf way to do it easely > just for output: > */ > > double round(double number, int decimalPlaces) > { > double dResult = number; > int nMultiplier = 1; > > for (int i = 0; i < decimalPlaces; i++) nMultiplier *= 10; > return (double)(int)(dResult * nMultiplier + .5) / nMultiplier; > } > > int main(int argc, char* argv[]) > { > printf("%f, %f", round(5.41, 1), round(5.45, 1)); > printf("%.1f, %.1f", 5.41, 5.45); > return 0; > } > > "Charles Tam" wrote: > > > I would like to round off a float value to one decimal place. > > > > How do I implement this correctly, so that, it would handle rounding up/down > > as required. > > > > For example, 5.41 becomes 5.4, and > > 5.45 becomes 5.5 > >
From: Jason Doucette on 10 Apr 2006 00:54 I would use the value 0.1, and determine the number of times it divides into your number, round this to the nearest integer, and multiply this integer by 0.1. This avoids the loop mentioned in the other post. If you need a generic function, you could compute 0.1, 0.01, etc. from the number of decimal digits required with the pow() function. I found it convenient to pass the actually number in; there was a time I needed a number rounded to the nearest 0.05. -- Jason Doucette www.jasondoucette.com
|
Pages: 1 Prev: ERROR_DLL_INIT_FAILED - HELP please Next: Problems using /MT switch |