The Method of Realizing Rounding and Remainder by C Programming

  • 2021-08-28 20:52:18
  • OfStack

In this paper, the method of rounding and remainder by C # programming is described with examples. Share it for your reference, as follows:

"%" is the surplus number, needless to say.
The "/" sign is now rounding in the shaping operation, and dividing in the floating-point operation, such as 54/10 with a result of 5, 54.0/10.0 with a result of 5.4, and rounding is not carried out with 4 rounding and 5 integers only, such as 54/10 and 56/10 with a result of 5.

Math. Celling () takes the larger number of integers, which is rounded up. It is equivalent to entering 1 digit no matter what the remainder is. For example, the result of Math. Celling (54.0/10.0) is 6.
Math. Ceiling (Convert. ToDecimal (d)). ToString () or string res = Math. Ceiling (Convert. ToDouble (d)). ToString (); res is 5 string res =
Math. Floor () takes the smaller number of an integer, which is rounded down. Equivalent to the "/" sign, that is, no matter what the remainder is, it is not carried. For example, the result of Math. Floor (56.0/10.0) is 5.
Math. Floor (Convert. ToDecimal (d)). ToString () or string res = Math. Floor (Convert. ToDouble (d)). ToString (); res is 4

The code is as follows:


using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication5
{ class Program { static void Main(string[] args)
{ Console.WriteLine("(54/10):{0}", 54 / 10);
Console.WriteLine("(56/10):{0}", 56/ 10);
Console.WriteLine("(54.0%10.0):{0}", 54.0 % 10.0);
Console.WriteLine("(56.0%10.0):{0}", 56.0 % 10.0);
Console.WriteLine("Math.Celling(54.0/10.0):{0}", Math.Ceiling(54.0 / 10.0));
Console.WriteLine("Math.Celling(56.0/10.0):{0}", Math.Ceiling(56.0 / 10.0));
Console.WriteLine("Math.Floor(54.0/10.0):{0}", Math.Floor(54.0 / 10.0));
Console.WriteLine("Math.Floor(56.0/10.0):{0}", Math.Floor(56.0 / 10.0)); } } }

In C #, there is one problem about the division "/" operation.

Now C # is related to the type of the value obtained by dividing "/" in method, which is related to the type of its divisor and dividend. Such as:


int a=4;
int b=5;
float c=a/b ;

The result is 0 (because the division of int will be performed first, resulting in 0, and then the result will be converted to float 0;) ;
In a word, the numbers obtained are all shaped, and finally it is found that the type of the value obtained after the original division is related to the type of his divisor and dividend. Therefore, it should be written as:


float a=3;
float b=5;
float c=a/b;

In this way, we can draw a correct conclusion!

I hope this article is helpful to everyone's C # programming.


Related articles: