Jump to content
  • Advertisement
Sign in to follow this  
stitchs_login

C# trouble obtaining a decimal value.

This topic is 1476 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Edited by frob

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!