Home > Net >  Algorithm confusion regarding the number conversion in C#
Algorithm confusion regarding the number conversion in C#

Time:09-17

I am following a tutorial regarding converting an integer number into a spoken-word equivalent in C#.

I am getting a bit confused about the three digit rule.

// Single-digit and small number names
private static string[] smallNumbers = new string[]
  {
    "zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
    "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
    "sixteen", "seventeen", "eighteen", "nineteen"
  };

// Tens number names from twenty upwards
private static string[] tens = new string[]
  {
    "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy",
    "eighty", "ninety"
  };

// Scale number names for use during recombination
private static string[] scaleNumbers = new string[]
  {
    "", "thousand", "million", "billion", "trillion",
    "quadrillion", "quintillion"
  };

public static string ToWords(this BigInteger number)
{
  
  if (number == 0)
  {
    return "zero";
  }

  
  int[] digitGroups = new int[groups];

  // Ensure a positive number to extract from
  var positive = BigInteger.Abs(number);

  // Extract the three-digit groups
  for (int i = 0; i < groups; i  )
  {
    digitGroups[i] = (int)(positive % 1000);
    positive /= 1000;
  }

  //The rest of the code.. 
}

I am assuming now we are converting a number and its value is 25111.

In the for-loop function, the return value of (int)(positive % 1000) should be 111. The 111 does not match any elements in digitalGroups array.

I don't quite get it, can someone explain it to me? Thanks in advance.

CodePudding user response:

The code you are showing us is not matching but rather assigning the value 111 to the first item of the digitGroupsArray.

How many items has digitGroupsArray? I don't know, it depends on the 'groups' variable value, which we can't see in the code excerpt.

Here:

int[] digitGroups = new int[groups];

you're creating a new empty integer array called digitGroups with the length of (int) 'group' value.

While this,

 for (int i = 0; i < groups; i  )
 {
     digitGroups[i] = (int)(positive % 1000);
     positive /= 1000;
 }

is a cylce, an iteration. And note that each time the 'positive' variable gets divided by 1000 (like positive = positive / 1000).

The result will be like this:

digitGroups[0] = (int)(25111 % 1000) // first item of digitGroupsarray
'positive' gets divided (25111 / 1000) next time will be 25
digitGroups[1] = (int)(25 % 1000) // second item of digitGroupsarray
'positive' gets divided again (25 / 1000) next time will be 0

and so on...

In these situation is very common and useful to log the values and debug the cycle. It really clears up your mind.

Working example:

static void Main(string[] args)
    {
        int groups = 3;
        int[] digitGroups = new int[groups];
        int positive = 25111;

        for (int x = 0; x < groups; x  )
        {
            Console.WriteLine("positive value is: "   positive);
            digitGroups[x] = (int)(positive % 1000);
            positive /= 1000;
            Console.WriteLine("item number (index): "   x   " value: "   digitGroups[x]);
        }
    }

Outputs:

positive value is: 25111
item number (index): 0 value: 111
positive value is: 25
item number (index): 1 value: 25
positive value is: 0
item number (index): 2 value: 0
  • Related