ok so I am practicing with a basic carpet calculator , but when I came midway into it I noticed the " (double) " data types , I know its used to shift a value's decimal places two places but in this case it is acting the same way how "double" in the third block of code (double roomLength,) is doing how the identifier's are all grouper under one data type. Is that what " (double) " is doing as well holding all those identifier's above and below it ?

``````const int SQ_FT_PER_SQ_YARD = 9;
const int INCHES_PER_FOOT = 12;
const string BEST_CARPET = "Berber";
const string ECONOMY_CARPET = "Pile";

int roomLengthFeet = 12 ,
roomLengthInches = 2,
roomWidthFeet = 13,
roomWidthInches = 7;

double roomLength,
roomWidth,
carpetPrice,
numOfSquareFeet,
numOfSquareYards,
totalCost;

roomLength = roomLengthFeet +
(double) roomLengthInches / INCHES_PER_FOOT;
roomWidth = roomWidthFeet +
(double) roomWidthInches /INCHES_PER_FOOT;
numOfSquareFeet = roomLength * roomWidth;
numOfSquareYards = numOfSquareFeet / SQ_FT_PER_SQ_YARD;

carpetPrice = 27.95;
totalCost = numOfSquareYards * carpetPrice;
Console.WriteLine("The cost of " + BEST_CARPET
+ " is {0:C}", totalCost);

Console.WriteLine();
carpetPrice = 15.95;
totalCost = numOfSquareYards * carpetPrice;
Console.WriteLine("The cost of " + ECONOMY_CARPET + " is"
+ "{0:C}", totalCost);

Console.Read();``````

thanks again !

## Recommended Answers

It is explicitly casting an integer to a double datatype. Dividing 2 integers yields undesired results as an integer divided by an integer will have an integer result (decimals get truncated). Go ahead and try it without the cast with values whose quotient is not a whole number.

It is converting the type of (in line 18/19) roomLengthInches into a double instead of an int. It needs to do this otherwise the division would be an integer division and would give wrong answers. For example, if roomLengthInches is anything less than 12, the result would be 0 (zero) …

## All 5 Replies

It is explicitly casting an integer to a double datatype. Dividing 2 integers yields undesired results as an integer divided by an integer will have an integer result (decimals get truncated). Go ahead and try it without the cast with values whose quotient is not a whole number.

It is converting the type of (in line 18/19) roomLengthInches into a double instead of an int. It needs to do this otherwise the division would be an integer division and would give wrong answers. For example, if roomLengthInches is anything less than 12, the result would be 0 (zero) as integer division doesn't round, it truncates.

Practical example:

``````double price;
price = 10 / 3;
price (double) 10 / 3;``````

Line 2 sets price equal to 3.0
Line 3 sets price equal to 3,333333333

Line 3 sets price equal to 3,333333333

It would if you had the = in there.;)

ohh! Ok thanks , that makes sense how double "encapsulates" (if it is safe to say :) ) the identifier's and allows "decimal" division instead of integer division (i had a few of those probems before... )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.