The digit grouping code doesn't show the decimal symbol in textbox

Karim Vazirinejad 186 Reputation points
2021-06-06T18:13:02.803+00:00

I have a calculator app that has a display like real calculators. Now I want to add digit grouping to it, that enhance the readability of the display. I added this code to my app, and it worked fine for integer numbers, but it doesn't show the fraction point in decimal numbers. I used double for defining the variables, but I don't know where the problem is.

private void txtDisplay_TextChanged(object sender, EventArgs e)
        {
            btnPoint.Enabled = !txtDisplay.Text.Contains(".");
            btnBackSpace.Enabled = Convert.ToBoolean(txtDisplay.Text.Length);

            //digit grouping the number in textbox

            if (txtDisplay.Text.Length > 0)
            {
                dblGroupedNumber = Convert.ToDouble(txtDisplay.Text);
                txtDisplay.Text = string.Format("{0:n0}", dblGroupedNumber);

            }
        }
Developer technologies C#
{count} votes

2 answers

Sort by: Most helpful
  1. WayneAKing 4,931 Reputation points
    2021-06-06T23:11:39.44+00:00

    it doesn't show the fraction point in decimal numbers.
    txtDisplay.Text = string.Format("{0:n0}", dblGroupedNumber);

    You appear to be suppressing the decimals with the
    string format. Did you try this?

    txtDisplay.Text = string.Format("{0:n2}", dblGroupedNumber);
    
    • Wayne

  2. WayneAKing 4,931 Reputation points
    2021-06-06T23:24:34.193+00:00

    Another possibility which shows significant trailing digits only:

    txtDisplay.Text = string.Format("{0:n9}", dblGroupedNumber).TrimEnd('0');
    
    • Wayne

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.