 # Multiplication of decimal and double, result? [Answered]RSS

## 8 replies

Last post Feb 28, 2017 09:56 PM by deepalgorithm

• ### Multiplication of decimal and double, result?

Feb 28, 2017 07:18 PM|JAYHAWKER|LINK

I assume when multiply a decimal by a double, you convert the double to decimal, not the other way around, Right?

• ### Re: Multiplication of decimal and double, result?

Feb 28, 2017 07:33 PM|mgebhard|LINK

Sounds right... Are you receiving an error or unexpected results?

• ### Re: Multiplication of decimal and double, result?

Feb 28, 2017 07:38 PM|deepalgorithm|LINK

As you probably know, you can't multiply a decimal  by a double. So yes you need to cast one to the other. Cast to decimal.

And always use decimal if you care about precision.  Decimal was designed to be useful for financial calculations since it offers high precision at the cost of reduced range for the size of the type in bytes.

From MSDN

"The decimal  keyword indicates a 128-bit data type. Compared to floating-point types, the decimal  type has more precision and a smaller range, which makes it appropriate for financial and monetary calculations. The approximate range and precision for the decimal type are shown in the following table."

Refer to the following if you want to read up on decimal and precision in more depth:

http://csharpindepth.com/Articles/General/Decimal.aspx

• ### Re: Multiplication of decimal and double, result?

Feb 28, 2017 08:59 PM|JAYHAWKER|LINK

Thanks for the replies, I am not getting any errors.  The whole "decimal" vs. float / dbl thing is a little confusing to me as to

What did you mean by, " it offers high precision at the cost of reduced range for the size of the type in bytes.". That may be the explanation I have been looking for.

• ### Re: Multiplication of decimal and double, result?

Feb 28, 2017 09:07 PM|deepalgorithm|LINK

I've updated my original answer.

• ### Re: Multiplication of decimal and double, result?

Feb 28, 2017 09:28 PM|JAYHAWKER|LINK

Last quick question.  Let's say you want to get a total dollar amount for square feet of 100.28 and a dollar value per square foot of 11.26.

Is it best to store and multiply both the square feet and dollar value as decimal?

• ### Re: Multiplication of decimal and double, result?

Feb 28, 2017 09:43 PM|JAYHAWKER|LINK

One of the things confusing to me is the Microsoft built-in function for calculating a payment uses "doubles" for the values.

(Microsoft.VisualBasic.Financial.Pmt())

• ### Re: Multiplication of decimal and double, result?

Feb 28, 2017 09:54 PM|mgebhard|LINK

As answered in your other thread with the same subject, use decimal for monetary calculations - that's what the type is for.

Double is still pretty accurate and for the PMT calculation it is probably fine.

If you want to know the technical reason, I suggest you take some time to read about the subject or take a few CS or engineering classes.

https://en.wikipedia.org/wiki/Floating_point

• ### Re: Multiplication of decimal and double, result?

Feb 28, 2017 09:56 PM|deepalgorithm|LINK

If numbers must add up correctly or balance, use decimal. This includes any financial storage or calculations, scores, or other numbers that people might do by hand.

Total dollar amount sounds like money and for money, always use decimal. It's why it was created.

If you want more evidence of why you should use decimal for money, then read this:

https://msdn.microsoft.com/en-us/library/system.decimal.aspx?f=255&MSPPError=-2147217396

"The Decimal value type represents decimal numbers ranging from positive 79,228,162,514,264,337,593,543,950,335 to negative 79,228,162,514,264,337,593,543,950,335. The Decimal value type is appropriate for financial calculations requiring large numbers of significant integral and fractional digits and no round-off errors. The Decimal type does not eliminate the need for rounding. Rather, it minimizes errors due to rounding."