Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

Is there a way to format a dynamically obtained number inside a while loop?

for example
C++
 while (csf.ReadString(buffer))
          {
int out;
 out = swscanf_s(buffer, _T("%f"), &lat);
double latitude = lat; 
// lat value is 1.0652337487801220, but I want to format this number to 9 decimal places and use this for further calculations inside this loop. 
}
Posted

I think you mean 'round', not 'format'.
Try this:

C++
	/////////////////////////////////////////////////////////////////////////////////////////////////////////
	// This class rounds a number to the closes multiple of Delta
	/////////////////////////////////////////////////////////////////////////////////////////////////////////

template <typename float_t="">
class Rounder
{
	float_t q1;
	float_t qh;
	float_t q;
public:
	Rounder(float_t delta): q(delta), q1(1.0 / delta), qh(delta * 0.5) {}

	float_t round(IN float_t f)
	{
		return q * floor( q1 * ( f + qh ) );
	}
};


#ifdef _DEBUG	
// Unit testing for class Rounder.
struct TestUtils
{
	void rounder(void)
	{
		if (!stdout)
			return;
		printf("TestUtils::rounder()\n");
		Rounder<double> milli(0.001);
		Rounder<double> penta(0.2);
		Rounder<double> eight(0.125);
		Rounder<double> full(1.0);
		printf("\n \n   Input      0.001       0.2       0.125       1.0     \n");
		     printf(" ---------- ---------- ---------- ---------- --------- \n");
		for (int i = 0; i < 10; i++)
		{
			double d = rand() * 1.0 / (rand());
			printf(" %10f %10f %10f %10f %10f \n",
				d, milli.round(d), penta.round(d), eight.round(d), full.round(d) );
		}
		printf(" \n \n");
	}
};
#endif
 
Share this answer
 
v2
Comments
Sandeep Mewara 18-Jun-12 9:54am    
5!
Sumal.V 18-Jun-12 10:47am    
yes rounding is the word! I din't quite understand this example(for my level) ..need sometime to go through.. but thanks
Hi,

swscanf_s function only format the number to string.
to format it,

C++
out = swscanf_s(buffer, _T("%.9f"), &lat);


and you get 9 decimal places, but it is a string, you can not do any math calculations (like 1.123456789 + 1 -> string + int).

To get 9 decimal places in a number, you can do:

C++
double initial = 1.123456789123456789;
double final = floor((1.123456789123456789*1000000000))/1000000000;


and you get final = 1.123456789

notice that you doing this, you will lost precision.
 
Share this answer
 
Solution 2 of Pablo is a good answer to your question. Nothing to add to that.

But let me ask you: Why do you want to round your variable to 9 decimals? Normally, it is preferable to do all your calculations with the highest possible precision and do the rounding when outputting values for human inspection.

So my recommendation is: Leave your variable as is comes from scanf and perform your further calculations. Then, when generating output, do things like this:

double lat = 20.78892736459; // just as an example
...
printf ("Latitude: %10.6f\n", lat);
    // the output result will be rounded to 6 decimals


That's usually way better than doing intermediate rounding of your internal variables.
 
Share this answer
 
Comments
Sumal.V 18-Jun-12 10:42am    
Yeah I have done that, but since I'm converting calculations that were in excel to c++, the calculations are based on the rounded off values and they differ from the dynamic values obtained..
nv3 18-Jun-12 14:17pm    
You are probably surprised to see that some value in your csv file show for example as 0.123456789 and after input by sscanf the double variable might show a value of 0.1234567889998. Don't be irritated by that. Not all decimal values have an exact representation in floating format and this longer value is the closest as you can get in double format to the decimal value given. Trying to round it to 9 decimal places will result in the same long value again, because THIS IS the best possible representation for 0.123456789.

I know it's a little weird at first. But believe me, rounding internal double value does not make sense in most cases.
Sumal.V 19-Jun-12 5:18am    
Exactly! since I work with latitudes and longitude values from csv files, I always encounter such problems!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900