-->
Purpose
CFloatEdit is a CEdit derived class providing support for editing currency values.. The user specifies the initial value, the number of decimals, the characters for the comma and for the decimal point. The implementation will keep a consistent format during the editing process.
Editing features
The user types the input digits in the edit field. The value associated with the control is updated every time the contents is changed. The format is also adjusted to match the new value. New commas are added / removed automatically.
Typing "-" will toggle the sign. The user can type that anywhere in the control, the sign will be placed in the appropriate position.
Changing any property for the field will determine the immediate update of the displayed information. The implementation keeps the appropriate position for the editing cursor when possible.
Pressing the decimal point character will align the currently typed value to the decimal point and will move the cursor to the first position after the decimal point
When created, the control automatically takes the system settings regarding the decimal point and the thousands separator.
Inserting CFloatEdit controls into the project
Insert the ogx or CFloatEdit.h and CFloatEdit.cpp files into the project
To add a CFloatEdit control into a dialog, just subclass the control or attach with the Class Wizard a variable of this control type. In both situations, do not forget to insert the appropriate include directive at the beginning of the file.
It is mandatory to initialize every CFloatEdit control with the method SetValue in the dialog or view initialization routine.
Property Default Values
Number of decimal digits 2
Comma character ","
Decimal point character "."
Commas visible YES
Start value It's mandatory to be set by the user
Method Prototypes
CFloatEdit(); Default constructor
virtual ~CFloatEdit(); Destructor
double GetValue(void); Get the current value stored on the field. These value is evaluated after each change in the current field
void SetValue(double dblValue); Set the current value stored in the field
int GetDecimals( void ); Get the number of the digits after the decimal point
void SetDecimals( int nDecimals ); Set the number of the digits after the decimal point
void EnableCommas(BOOL bNewHasCommas); Enable / disable the visibility of the comma delimiters
void SetComma(char cNewComma); Set the comma character
char GetComma(void); Get the comma character
void SetDecimalPoint(char cNewDecimal); Set the decimal point character
char GetDecimalPoint(void); Get the decimal point character (default ".")
Important things to be mentioned
The code works on MS Visual C++ version 4.2 or newer
Every CFloatEdit control must be initialized at the beginning, with a SetValue statement (e.g., m_FloatEditPrice.SetValue(0.0); ).
The control value is stored in a double variable. User must take into account the internal precision capabilities
The decimal point character is set by default to "." and the comma character to ",". These characters can be changed, but setting the same value for both will determine unpredictable behavior
Download demo project - 18 KB
Download source - 6 KB
Date Last Updated: April 3, 1999
Comments
Bug when CFloatEdit is associated a member variable
Posted by vcarrenos on 09/09/2006 01:34pmHaving problem getting number until decimal is entered
Posted by efultz on 12/23/2004 04:19pmI am having a problem. If I enter 34 I only get returned by the GetValue function 3. If I enter 341 I am returned 34. BUT, if I press the decimal then I am returned the full 341. Any suggestions on how to fix this? It makes the control far less useful - to the point where I might have to remove it. I don't want to do that because it provides lots of other good things.
ReplyHow to Set the number of the digits before the decimal point
Posted by Legacy on 02/13/2004 12:00amOriginally posted by: Maria
ReplyBug with the decimal places
Posted by Legacy on 11/11/1999 12:00amOriginally posted by: Alpesh Makwana
ReplyCFloatEdit bug?
Posted by Legacy on 08/17/1999 12:00amOriginally posted by: Kent Tsai
I found a somewhat strange behavior with the control. The thing is that say, the current value is ".000" and I try to change it to "-5.000". I did this by moving cursor to the very beginning and enter negative sign, "-", and then "5". Rather than display "-5.000" as I would expect, the control shows ".500." Anthor related and interesting behavior is that is there is non-zero fraction number like ".100", then the value will come out correctly. I tried to fix this problem but couldn't. Any idea how to fix it?
ReplyEN_CHANGE
Posted by Legacy on 07/27/1999 12:00amOriginally posted by: Piero Taliani
Hi
I successfully use your CFloatEdit class. But I modified the setStringAndSelection member in this way:
void CFloatEdit::setStringAndSelection(CString strNew, int nSelection)
{
// recalculate the value for m_dblCurrentValue
CString strWork = strNew;
CString strInteger, strDecimal;
// will work by default with the decimal point
removeCommas(strWork);
getIntegerAndDecimal(strWork, strInteger, strDecimal);
strWork = strInteger + CString(".") + strDecimal;
m_dblCurrentValue = atof(strWork.GetBuffer(1));
SetWindowText(strNew.GetBuffer(1));
// adjusting the selection value
nSelection = nSelection < 0 ? 0 : nSelection;
nSelection = nSelection > strNew.GetLength() ? strNew.GetLength() : nSelection;
SetSel(nSelection, nSelection);
}
because the SetWindowText sends a EN_CHANGE event.
In your version, if you use at this point the GetValue member in the handler of the EN_CHANGE you have an unsyncronized value because the event arrive before the m_dblCurrentValue member is correctly setted. So moving the code
SetWindowText(strNew.GetBuffer(1));
// adjusting the selection value
nSelection = nSelection < 0 ? 0 : nSelection;
nSelection = nSelection > strNew.GetLength() ? strNew.GetLength() : nSelection;
SetSel(nSelection, nSelection);
after the
m_dblCurrentValue = atof(strWork.GetBuffer(1));
in the OnChangeXXXX you get the right one.
Bye
Replyererqr
Posted by Legacy on 06/22/1999 12:00amOriginally posted by: SARBENDU PAUL
Reply_ecvt and atof
Posted by Legacy on 05/26/1999 12:00amOriginally posted by: Gregor Bereznicki
ReplyRounding Bug - try typing 123.123!
Posted by Legacy on 04/11/1999 12:00amOriginally posted by: Amy Taylor
Probably caused by _ecvt!
Reply
Variables
Posted by Legacy on 02/05/1999 12:00amOriginally posted by: Tom Labotka
When I try naming a variable (e.g., CFloatEdit m_price) I am getting errors
asking for a ';' before m_price. Would you know what I am missing. Also,
when I use class wizard to name a variable, I am unable to enter CFloatEdit
as the variable type.
ReplyLoading, Please Wait ...