1

I am learning casting and conversion techniques and I thought I had the hang of it until i came across this. I know it is a way to divide a int by a decimal but can someone break it down how it works in simple terms? Cant seem to get my head around this type of cast. Thank you

int value1 = 12;
decimal value2 = 6.2m;
float value3 = 4.3f;

int result1 = (int)((decimal) value1 / value2);

Output:

Result1 = 1 
Martin
  • 16,093
  • 1
  • 29
  • 48
Jaybizl
  • 23
  • 5
  • 1
    What is `value1`? – Sweeper Dec 10 '19 at 13:55
  • 5
    The decimal cast is redundant since `value2` is a decimal already – Biesi Dec 10 '19 at 13:56
  • 8
    It casts `value1` to `decimal` and the result of `value1 / value2` to `int`. Can you elaborate where exactly you are stuck? – Marvin Dec 10 '19 at 13:57
  • 2
    @BiesiGrr but `value1` isn't. – Johnathan Barclay Dec 10 '19 at 13:58
  • If both `value1` and `value2` were of type `int`, the `(decimal)` cast would be needed because otherwise, it would cause an [integer division](https://stackoverflow.com/q/10851273/8967612). Whoever wrote that code probably was just too careful trying to avoid integer division so they used an unnecessary cast. The `(int)` cast, on the other hand, is just to cast the end result to an `int`. – 41686d6564 stands w. Palestine Dec 10 '19 at 13:58
  • I think your question is about the int cast? `value1 / value2` results in `1.93...` The int cast now truncates the decimal, so `1` is left . @CodeCaster it specifically does not round, it truncates. – FeRaaC Dec 10 '19 at 13:59
  • 2
    @JohnathanBarclay: value1 will automatically be cast to decimal due to value2 being decimal - so the explicit cast to decimal is redundant – PaulF Dec 10 '19 at 14:01
  • 2
    @JohnathanBarclay `(int / decimal)` type is `decimal` – Cid Dec 10 '19 at 14:01
  • It's a [numeric cast](https://www.dotnetperls.com/numeric-casts) – Matthew Watson Dec 10 '19 at 14:02
  • What is your actual problem here? Is it the decimal cast or the int cast, or the combination of both? – Reinstate Monica Cellio Dec 10 '19 at 14:10
  • @ARCHER It was the decimal cast that was confusing me, this solution to a challenge was provided by microsoft, was confused as to why they would of done it this way and wanted clarifaction – Jaybizl Dec 10 '19 at 14:35
  • @Jaybizl Okay - thanks for clarifying. Just wanted to make sure you got an answer to your actual problem :) – Reinstate Monica Cellio Dec 10 '19 at 15:01

1 Answers1

5

Let's start with something important. Casting value1 to decimal is obsolete, you get the same exact result from:

int result1 = (int)(value1 / value2);

You can see the result of not casting to int by doing this:

var result1 = (value1 / value2);
Console.WriteLine(result1.GetType());

Result: System.Decimal

If you try to assign that directly to int you get

Cannot implicitly convert type 'decimal' to 'int'. An explicit conversion exists (are you missing a cast?)

So you need to explicitly cast the decimal to int to assign it to result1

Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • The reason you get the same even without the decimal cast is that the compiler automatically does it for you. See [here](https://sharplab.io/#v2:CYLg1APgAgTAjAWAFBQMwAJboMLoN7LpGYZQAs6AsgBQCU+hxTAlgHYAu6AbgIYA2AVwCmcdAF50cGAG5GTIsCEBjZgFt+3fsJjj0ANgB0MVbKTzibTgCchAZwF9OE6pdrVegkegD0mzzFpTcyIoOABOaht7R0C5dABfZHigA===) – Biesi Dec 10 '19 at 14:07
  • 1
    @BiesiGrr `compiler automatically does it for you`, yeah, but IMHO we should be handling this. – Trevor Dec 10 '19 at 14:08
  • This was a solution provided by Microsoft to a challenge IMO I thought there is no need to use the decimal cast but just wanted clarification. – Jaybizl Dec 10 '19 at 14:33