-4

I'm creating a winform program in visual studio 2015 to calculate Pi, but I'm having trouble trying to get the number of logical digits.

I need to make it so that:

public decimal pi = 3.15195647857832565; //random decimals
txtDigits.Text = //number of digits in pi variable and get rid of trailing decimal points
Mare_413
  • 450
  • 5
  • 13

2 Answers2

-2

If you need to get the number of logical digits, IE stripping out trailing zeroes I suggest you use the accepted approach here:

Find number of decimal places in decimal value regardless of culture

decimal argument = 123.456m;
int digitCount = BitConverter.GetBytes(decimal.GetBits(argument)[3])[2];

I've used this approach in multiple projects and it works really nicely. Note that this is for finding the number of trailing decimal points (I assumed that's what you're after but it's not clear from your question).

Community
  • 1
  • 1
Jesse Carter
  • 20,062
  • 7
  • 64
  • 101
  • seams to work fine, thanks – Mare_413 Nov 25 '16 at 22:10
  • @user87073 `decimal argument = 1m/3;` returns 28. why not 29? or why not infinite? Improve your question.... And answer my comment posted ~48 mins ago... – L.B Nov 25 '16 at 22:40
  • @L.B Curious to know why this was downvoted? Seems odd to punish people for attempting to answer this question with high quality references just because OP didn't clarify exactly what he was after. I stated here that I *assumed* he was looking for trailing decimals. The method I've supplied here does exactly that. Or maybe you're just having a bad day? – Jesse Carter Nov 28 '16 at 18:00
-2

This will return the count of all the digits:

int i= 0;
foreach (char c in pi.ToString())
{
    if (Char.IsDigit(c))
        {
            i++;
        }
}
ManuM
  • 9
  • 4