Click here to Skip to main content
13,097,012 members (82,620 online)
Rate this:
Please Sign up or sign in to 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:
  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:
560988.92 it is rounded off to: 560989

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 1-Aug-12 5:17am
Richard MacCutchan 1-Aug-12 12:02pm
How are you displaying these values in the edit control?
Member 8446342 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?
Member 8446342 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
Member 8446342 2-Aug-12 6:56am
These programs are pre written/defined and I cannot alter anything in there. But yes I did have a look at the link you've sent, but don't quite understand what is wrong.
Richard MacCutchan 2-Aug-12 8:21am
The format string is %%.%dlf which is meaningless. I suggest you go to whoever wrote this class and get them to fix it.
Member 8446342 2-Aug-12 8:52am
Philip Stuyck 1-Aug-12 13:23pm
what is a CV3 ?
I have the feeling the problem is caused by using a float or a double. This kind of variable is not able to store all values exactly but something close to the desired value. This can sometimes be a problem.
Member 8446342 2-Aug-12 4:21am
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)
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
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
Member 8446342 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)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 2 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100