Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,
In my program, there is an edit box, where the user can type in the values. This value is used in some functions. But as soon as I close my s/w and reopen, this value is rounded off.
Even though the actual values typed is taken for calculations, this rounding off of values is kind of misleading/confusing the user.
How can I get rid of this?

The code for the edit button:
C++
  InsCtrl(80, EC_EDIT,m_userENZ1.x);
//m_userENZ1 is CV3 type.
// EC_EDIT is declared in a header file as :- const int EC_EDIT = 0x000040;
If the user enters a value:
C++
560988.92 it is rounded off to: 560989

C++
6767202.49 it is rounded off to: 6.7672e+006

Though these values do not affect the calculations,I need to get rid of this.
Need some help please..
Posted
v2
Comments
Richard MacCutchan 1-Aug-12 12:02pm    
How are you displaying these values in the edit control?
Sumal.V 2-Aug-12 4:28am    
I didn't get you.. it is displayed using :
InsCtrl(80, EC_EDIT,m_userENZ1.x);

where InsCtrl is a method in the class - CECtrl
InsCtrl(int c, int type, double& val, CEFlex* pFlex)
Richard MacCutchan 2-Aug-12 5:15am    
And how does this class format its numbers to display them?
Sumal.V 2-Aug-12 5:22am    
In this particular class, there is a settings function, where the object is added:-
obj.Item(_T("Source_Easting"), m_srcENZFx.x, 10);
There is another function in another main class where this function lies-

void CESetObj::Item(const CString& itm, double& val, int dp)
{ CString str, fmt;
fmt.Format(_T("%%.%dlf"), dp);
Add(itm, fmt, val)
}.
Richard MacCutchan 2-Aug-12 5:33am    

It seems you are storing the value that is typed into the edit control as a float/double.

There is always a potential loss of precision and/or change of representation if you convert from a string to a float/double and the convert back.

If you want to preserve exactly what's typed, then you need to store the value of the edit control as a string and save and restore that string -- then only convert it to a float/double when you do your calculation.
 
Share this answer
 
Comments
Kenneth Haugland 1-Aug-12 18:20pm    
GIve you a 5 for this one
pasztorpisti 1-Aug-12 18:41pm    

float f = 560988.92f;
printf("%f\n", f);

This code prints 560988.937500 on my machine. As the integer part grows, the fractions become less and less accurate as the answer says but we still have some accuracy in this example. The example with 6767202.49 gives very strange result for the asker, even the integer part is rounded in his example...
TRK3 1-Aug-12 18:53pm    
What's your point?

560988.92 = 560988 to 6 digits.

6767202.49 = 6767200 = 6.7672e6 to 6 digits.

We don't know what actual data type a CV3 is, but we can safely conclude that convert between a string and CV3 representation preserves approximatesly 6 digits of accuracy.

For all we know it's an 8 byte BCD encoded value with 2 digits of exponent and 6 digits of significand.

Or it's a 24 bit floating point number with 8 bits of exponent and 16 bits of significand.
pasztorpisti 1-Aug-12 19:56pm    
My bad! I was thinking in float! :-)
Then this problem is solved. +5
Sumal.V 2-Aug-12 4:31am    
CV3 in my program is a class with one of its method- CV3(double x, double y, double z).This is mainly used in calculations involving latitude, longitude and height.(3 parameters)
Instead of directly declaring the variable in the edit box, it is called from the main edit class;Also since the EC_EDIT is a const int variable, I explicitly declared the number of decimal places.

C++
CEEdit*  east;
east = (CEEdit*)InsCtrl(157, EC_EDIT, m_userENZ1.x);
east->decm = 3;

The decm is an int variable in CEEdit class, which is used for the number of decimal places. Here, defining the number of places as 3, it retains 3 decimal places, without rounding off.
 
Share this answer
 

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