question

Amir-1400 avatar image
0 Votes"
Amir-1400 asked WayneAKing-0228 commented

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

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);
    
             }
         }


dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What does it look like now?
WayneAKing's suggestion should be effective:

102966-4.png

I think the problem may occur when assigning a value to dblGroupedNumber.
Try to add a breakpoint on line 11 of the above code and observe the value of dblGroupedNumber at this time.
If possible, it is best to add the code that assigns dblGroupedNumber to the original post so that we can better analyze the problem.

0 Votes 0 ·
4.png (7.0 KiB)
WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered WayneAKing-0228 commented

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

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Or omit the precision specifier altogether:

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

0 Votes 0 ·

Dear Wayne,

Unfortunately, it doesn't work and make the case more complex.

0 Votes 0 ·

Just saying "it doesn't work" tells us nothing that we
can use to do further analysis. All you are telling us
is that the display does not match what you are expecting.

You have not told us:

What is shown.

What you are expecting/wanting to see.

What the data is that does not display as you want.

What purpose and role is served by the variable
"btnPoint.Enabled".

If you want help then you are going to have to put a
lot more effort into explaining what is happening.

  • Wayne

0 Votes 0 ·
WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered Amir-1400 commented

Another possibility which shows significant trailing digits only:

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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It doesn't work too.

0 Votes 0 ·