CFloatEdit - edit control for currency numbers


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame



  • 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


    • Bug with big double value

      Posted by ymam on 10/19/2015 07:42pm

      Hi if I input the value: 123456789,24 it's fine but one more digit and bug happens: it displays: 1234567890,23 What happens!!

    • Bug when CFloatEdit is associated a member variable

      Posted by vcarrenos on 09/09/2006 08:34pm

      This control fail when is asociated a member variable and use DDV and DDX control mechanisme, show error message box when dialogs init

    • Having problem getting number until decimal is entered

      Posted by efultz on 12/24/2004 12:19am

      I 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.

    • How to Set the number of the digits before the decimal point

      Posted by Legacy on 02/13/2004 08:00am

      Originally posted by: Maria


      How can i set the number of digits to be accepted bfore
      the decimal point.

    • Bug with the decimal places

      Posted by Legacy on 11/11/1999 08:00am

      Originally posted by: Alpesh Makwana

      I have successfully implemented your code and it works very much with the ease but there is one bug in it. that i am not able to solve i.e. when i try typeing "22" in the decimal place it gives me an output of "12" in the decimal places. also the same thing happens when i try to type "77"
      it gives me "67" on the display. please infrom me about the solution for this bug.
      Another interesting thing about this control is that how do we set the maximum limit of the decimal places and the actual no. supposing i want the control to accept something like "12.23" how do i go about doing this things.

      This was a great control thanks for doing this kind of work.
      Please carry on the good work.

    • CFloatEdit bug?

      Posted by Legacy on 08/17/1999 07:00am

      Originally 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?


      Posted by Legacy on 07/27/1999 07:00am

      Originally posted by: Piero Taliani


      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
      getIntegerAndDecimal(strWork, strInteger, strDecimal);
      strWork = strInteger + CString(".") + strDecimal;

      m_dblCurrentValue = atof(strWork.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

      // 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.


    • ererqr

      Posted by Legacy on 06/22/1999 07:00am

      Originally posted by: SARBENDU PAUL


    • _ecvt and atof

      Posted by Legacy on 05/26/1999 07:00am

      Originally posted by: Gregor Bereznicki

      Try this one:
      position the caret before the comma, press '1' - and you get 11,13 !!!
      when you set MAX_LENGTH to 15, you get other problems with bigger numbers. Why not to use this one:

      _fcvt( dblValue, m_nDecimals, &nDecimalPosition, &nSign )

      It handles rounding correctly (maybe not in Italy... :-)

      The second suspicious place is a call to atof(), according to MS "...The output value is affected by the setting of the LC_NUMERIC category in the current locale. ..." -
      it's not secure to replace a decimal point character with a hardcoded '.'...

      Anyhow - happy computing!

    • Rounding Bug - try typing 123.123!

      Posted by Legacy on 04/11/1999 07:00am

      Originally posted by: Amy Taylor

      Probably caused by _ecvt!

    • Loading, Please Wait ...

    • You must have javascript enabled in order to post comments.

    Leave a Comment
    • Your email address will not be published. All fields are required.

    Most Popular Programming Stories

    More for Developers

    RSS Feeds

    Thanks for your registration, follow us on our social networks to keep up-to-date