CFloatEdit - edit control for currency numbers

-->

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:34pm

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

      Reply
    • Having problem getting number until decimal is entered

      Posted by efultz on 12/23/2004 04:19pm

      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.

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

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

      Originally posted by: Maria

      Hi,
      
      

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

      Reply
    • Bug with the decimal places

      Posted by Legacy on 11/11/1999 12: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.
      Alpesh

      Reply
    • CFloatEdit bug?

      Posted by Legacy on 08/17/1999 12: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?

      Reply
    • EN_CHANGE

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

      Originally 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


      Reply
    • ererqr

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

      Originally posted by: SARBENDU PAUL

      Good
      

      Reply
    • _ecvt and atof

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

      Originally posted by: Gregor Bereznicki

      Try this one:
      
      1,14
      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!

      Reply
    • Rounding Bug - try typing 123.123!

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

      Originally posted by: Amy Taylor

      Probably caused by _ecvt!

      Reply
    • Variables

      Posted by Legacy on 02/05/1999 12:00am

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

      Reply
    • Loading, Please Wait ...

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

    Top White Papers and Webcasts

    • Live Event Date: August 13, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT If you are developing applications, you'll want to join us to learn how applications are changing as a result of gesture recognition. This technology will change how you and your users interact - not simply with your devices, but with the world around you. Your devices will be able to see and hear what your users are doing. Are your applications ready for this? Join us to learn about Intel® RealSense™ Technology, including never been …

    • In this on-demand webcast, Oracle ACE and Toad Product Architect Bert Scalzo discusses 10 powerful and hidden features in Toad® that help increase your productivity and DB performance. Watch this webcast today.

    Most Popular Programming Stories

    More for Developers

    Latest Developer Headlines

    RSS Feeds