C# trouble obtaining a decimal value.

Started by
3 comments, last by Pink Horror 9 years, 6 months ago

Hi all,

I am working through book exercises for C#. And one of the questions is to convert a monetary amount, represented by a double.

I have encountered a small error when I try to extract the penny value from this Double value:

for example:

I input 47.32. I use the formula/code:


int penniesToConvert = (int)((moneyIn - Math.Truncate(moneyIn)) * 100);

This takes the double value, subtracts the Integer part, multiplies by 100 to make .32 into the Integer 32, and casts it to be placed in a new Integer type. This is then used for calculations later.

Until now, I assumed this was fine, and it gave me an Integer value of 32. I changed the penny value of the original double to test it; 13, 47, 63 etc.

It's only when I input .33, that the Integer I get back is 32. I extracted the part of moneyIn - Math.Trunc... (without multiply by 100) and when I look through the debugger, I get a value of .3299999999... Which, when multiplied and casted, becomes 32. The same happens with the value 66. Why is this, is there anyway to account for these, or is my method of extracting the penny value incorrect here.

On a sidenote, this is only in Chapter 2 of the book, so I can't imagine that the solution should be too complicated.

Any help on this would be much appreciated.

Stitchs.

Advertisement

Just like base 10 you are used to, many numbers cannot be directly represented in any fractional number system. As an easy example, 1/3 cannot be directly represented in base 10. You get 0.33333. You know 3 * 1/3 is 1, so you aren't too surprised to see 3 * 0.33333 = 0.99999 meaning 1.

Numbers in computers operate in base 2. That means for fractional number, the first behind the decimal point is 1/2, the second is 1/4, then 1/8, then 1/16. The result ultimately is an approximation. In base ten your decimal parts have 1/10 + 1/100 + 1/1000, in base 2 you have a similar 1/2 + 1/4 + 1/8 + 1/16.

Whenever you use floating point numbers you should assume the values are an approximation. They are inexact, just like base 10. You're just used to it when you see it in base 10.

http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

When you start to really use floating numbers they will always have errors in precision (up to a point).

In this case you could just use rounding, and the problem will go away.

Hi, thanks for the responses.

I see what you are saying, maybe I'm a bit out of my league on understanding the ins-and-outs of it.

I looked into the suggestion of rounding the approximation, to that of one decimal place. It seems to have worked. I wrapped my previously explained method in a Math.Round() function.

I have started reading the suggested article, hopefully this will open my mind a bit!

Thanks again,

Stitchs.

C# has a type named decimal that's meant for currency.

http://msdn.microsoft.com/en-us/library/364x0z75%28VS.80%29.aspx

This topic is closed to new replies.

Advertisement