TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Divide Numbers (Cast Ints)

Understand the division operator, and evaluate casts for dividing numbers.
Divide. Division uses the forward-slash character. With division, one common problem is related to correctly casting the operands in the division.
With correct syntax we get a double from the division of 2 ints. But if we do not cast an int in the expression, the result may be truncated (and not useful for us).Int, uintCasts
An example. The program declares 2 Int32 variables and then divides the first by the second in 5 different operations. The operations differ in lexical syntax.

Casts: In the first and third assignments, there were no casts on the integers or the entire division was cast at once.

Double

Tip: To coerce the intermediate language to have the correct casts, we cast either operand (number2 and number5) or both operands.

And: When either operand or both operands are cast to double, the output is approximately 0.29.

C# program that divides numbers using System; class Program { static void Main() { // Divide the first number by the second number. int operand1 = 100; int operand2 = 345; // Incorrect division for double: double number1 = operand1 / operand2; Console.WriteLine(number1); // Correct division for double: double number2 = (double)operand1 / operand2; Console.WriteLine(number2); // Incorrect division for double: double number3 = (double)(operand1 / operand2); Console.WriteLine(number3); // Correct division for double: double number4 = (double)operand1 / (double)operand2; Console.WriteLine(number4); // Correct division for double: double number5 = operand1 / (double)operand2; Console.WriteLine(number5); } } Output 0 0.289855072463768 0 0.289855072463768 0.289855072463768
Discussion. The C# language specification provides the definition of all its predefined operators. It describes these predefined operators in the same way as real methods.

So: You can think of the 2 operands on each division expression as parameters to a method with a custom syntax.

Discussion, continued. The C# compiler removes the cast from the number3 assignment. For the 3 assignments where one or both operands are casts, it inserts casts to both operands.

Note: The complexity of this situation is mainly because of the rules by which the C# compiler interprets casts in the source code.

Numeric promotion. In numeric promotion, a parameter to an operator or method can be implicitly converted to match the operator or method signature.

And: All the parameters will be promoted to match the signature. This is an algorithm the C# compiler uses to ensure more programs compile.

Modulo. A modulo operator implements remainder division. This might remind you of doing long division in school, where you must compute the "leftover" part after dividing 2 numbers.Modulo
A summary. We used the casting syntax in division to ensure the correct number of decimal places. You need to cast one or both operands to get a double type.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf