PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 24, 2022

[FIXED] How can I use Math Ceiling RoundUp How to determine even odd number

 October 24, 2022     c#, ceil, decimal, modulo     No comments   

Issue

I need to print data from a DataGridView on both sides of a preprinted form but:

  1. Each side has different arrangement for that info.
  2. Each side can only hold info from tree rows, so:
  3. 1st, 2nd and 3rd row go on side 1;
  4. 4th, 5th and 6th row go on side 2;
  5. 7th, 8th and 9th row go on side 1;
  6. 10th, 11th and 12th go on side 2; and so on.

I will select which group to print.

I’m planning to do it this way: enter image description here

  1. ((row.Index) +1) / 3,
  2. round it up, with no decimals, to get an integer, (like in the above excel image),
  3. MOD that integer by 2, (like in the above excel image).

If the result of that MOD by 2 is 1, then it will print Side 1 arrangement, if the result of that MOD by 2 is 0, then it will print Side 2 arrangement.

  • How do I do it in C#? I'm using VS2010 Express Edition. Also, I wanted to use System.Math.Ceiling but I get a Namespace, decimal, double-precision and floating-point number warnings or errors.

Solution

I don't see that you need to use anything like that:

int zeroBasedRow = row - 1;
int side = ((zeroBasedRow / 3) % 2) + 1;

Test code:

using System;

class Test
{
    static void Main(string[] args)
    {
        for (int row = 1; row <= 12; row++)
        {
            int zeroBasedRow = row - 1;
            int side = ((zeroBasedRow / 3) % 2) + 1;
            Console.WriteLine("Row {0} goes on side {1}", row, side);
        }
    }
}

Output:

Row 1 goes on side 1
Row 2 goes on side 1
Row 3 goes on side 1
Row 4 goes on side 2
Row 5 goes on side 2
Row 6 goes on side 2
Row 7 goes on side 1
Row 8 goes on side 1
Row 9 goes on side 1
Row 10 goes on side 2
Row 11 goes on side 2
Row 12 goes on side 2


Answered By - Jon Skeet
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing