Re: Formatting numeric data in textbox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mihai N.

    Re: Formatting numeric data in textbox

    I need to format any decimal value so that it is right aligned, thousands
    are separated and shows 2 decimal places always.
    It is good to show the info in the format the user prefers.
    That is language/locale speciffic (CultureInfo in the .NET lingo)
    So you should use something like ToString("N")
    If you are sure you want 2 digits after decimal separator, then use "N2"



    --
    Mihai Nita [Microsoft MVP, Visual C++]
    Best internationalization practices, solving internationalization problems.

    ------------------------------------------
    Replace _year_ with _ to get the real email
  • Andrus

    #2
    Re: Formatting numeric data in textbox

    Mihai,
    It is good to show the info in the format the user prefers.
    That is language/locale speciffic (CultureInfo in the .NET lingo)
    So you should use something like ToString("N")
    If you are sure you want 2 digits after decimal separator, then use "N2"
    Thank you.

    I need to format numbers when form is initially shown and after user leaves
    changed TextBox.
    Which TextBox event should be used for this ? Where ToString("N2") call
    should be placed ?

    Andrus.

    Comment

    • Mihai N.

      #3
      Re: Formatting numeric data in textbox

      I need to format numbers when form is initially shown and after user leaves
      changed TextBox.
      Which TextBox event should be used for this ? Where ToString("N2") call
      should be placed ?
      I personally try to stay away from Mask.
      I initialize the fields when I create the form (using ToString),
      and I validate them when user tries to submit the data
      (using Parse or TryParse)


      --
      Mihai Nita [Microsoft MVP, Visual C++]
      Best internationalization practices, solving internationalization problems.

      ------------------------------------------
      Replace _year_ with _ to get the real email

      Comment

      • Andrus

        #4
        Re: Formatting numeric data in textbox

        I personally try to stay away from Mask.
        I initialize the fields when I create the form (using ToString),
        and I validate them when user tries to submit the data
        (using Parse or TryParse)
        I do'nt understant this.
        In the sample code I posted Textbox is data bound to decimal property Amount
        which has value 12
        Winforms initializes this TextBox automatically.

        How to force Textbox to show 12.00 when form is shown and TextBox does not
        receive focus ?

        DataGridView column has DefaultCellStyl e.Format property for this.
        I havent found .Format property when TextBox is not in DataGridView.

        Andrus.

        Comment

        • Mihai N.

          #5
          Re: Formatting numeric data in textbox

          In the sample code I posted Textbox is data bound to decimal property
          Amount which has value 12
          Winforms initializes this TextBox automatically.
          I am not sure if Winforms uses the CultureInfo for that (to use the proper
          decimal/thousand separators).
          If it does, then just let Winforms do the work.

          Otherwise, you might have to capture the loss of focus and reformat the way
          you want.

          Personally (as user), I don't care much about stuff being reformated.
          If I type .2 and remains like this, I'm ok with it.
          But I do care a lot about separators (123,456 means a completely different
          thing in Germany vs US)



          --
          Mihai Nita [Microsoft MVP, Visual C++]
          Best internationalization practices, solving internationalization problems.

          ------------------------------------------
          Replace _year_ with _ to get the real email

          Comment

          • Andrus

            #6
            Re: Formatting numeric data in textbox

            >In the sample code I posted Textbox is data bound to decimal property
            >Amount which has value 12
            >Winforms initializes this TextBox automatically.
            >
            I am not sure if Winforms uses the CultureInfo for that (to use the proper
            decimal/thousand separators).
            If it does, then just let Winforms do the work.
            The issue is not related to cultureinfo.
            Isse is that it is not possible to show 12 with 2 decimal places when form
            is opened.
            Otherwise, you might have to capture the loss of focus and reformat the
            way
            you want.
            >
            Personally (as user), I don't care much about stuff being reformated.
            If I type .2 and remains like this, I'm ok with it.
            But I do care a lot about separators (123,456 means a completely different
            thing in Germany vs US)
            My issue is that my program calculates product cost etc. using 5 digits
            after comma and this must remain.
            Users want to see this value with 2 digits after comma and also to enter
            this value.
            DataGridView DefaultCellStyl e has Format property which allows this but
            there is no such property when TextBox is placed to form.

            Andrus.

            Comment

            • Mihai N.

              #7
              Re: Formatting numeric data in textbox

              The issue is not related to cultureinfo.
              Not quite. CultureInfo gives you the right decimal separator
              (which is a good thing).
              Isse is that it is not possible to show 12 with 2 decimal places when form
              is opened.
              ToString("N2")
              DataGridView DefaultCellStyl e has Format property which allows this but
              there is no such property when TextBox is placed to form.
              Than you have to do it yourself by calling ToString when the control looses
              focus.


              --
              Mihai Nita [Microsoft MVP, Visual C++]
              Best internationalization practices, solving internationalization problems.

              ------------------------------------------
              Replace _year_ with _ to get the real email

              Comment

              • Andrus

                #8
                Re: Formatting numeric data in textbox

                Mihai,
                >DataGridView DefaultCellStyl e has Format property which allows this but
                >there is no such property when TextBox is placed to form.
                Than you have to do it yourself by calling ToString when the control
                looses
                focus.
                When form is initially shown, control does not lose focus.
                So there is no place to call ToString().

                Andrus.

                Comment

                • Mihai N.

                  #9
                  Re: Formatting numeric data in textbox

                  When form is initially shown, control does not lose focus.
                  So there is no place to call ToString().
                  Then you might have to do the same at creation,
                  not only at lose focus.


                  --
                  Mihai Nita [Microsoft MVP, Visual C++]
                  Best internationalization practices, solving internationalization problems.

                  ------------------------------------------
                  Replace _year_ with _ to get the real email

                  Comment

                  Working...