Click here to Skip to main content
15,899,549 members
Please Sign up or sign in to vote.
4.33/5 (3 votes)
See more:
please see the code snippet...
C#
str="44.22";
double d = Convert.ToDouble(str);
Console.WriteLine("the d val" + d);
int doll;
int cent;
doll=(int)d;
Console.WriteLine(doll);      //44
cent =(int)( (d - doll) * 100);
Console.WriteLine(cent);     //21

In the output of 'cent' why it is 21,why not 22.

if i am using
C#
Convert.Int32((d-doll)*100);
then output will be 22.
Posted
Updated 11-Apr-12 3:37am
v3
Comments
Frank R. Haugen 11-Apr-12 6:37am    
I tested it and your right, and it's just weird, I would say rounding error but there's nothing to round out.
samu4u 11-Apr-12 9:57am    
I think you are correct...rounding error...
Frank R. Haugen 12-Apr-12 11:43am    
Not to be a stickler but how about accepting one or two answers

As said in solution 2 by Richard MacCutchan decimal precision is a problem with float and double. For high precision decimal Data Type can be used.
Please check the following code.
C#
string str="44.22";
decimal d = Convert.ToDecimal(str);
Console.WriteLine("the d val = " + d);
int doll;
int cent;
doll=(int)d;
Console.WriteLine(doll);      //44
cent =(int)( (d - doll) * 100);
Console.WriteLine(cent);     //22
 
Share this answer
 
v2
Comments
samu4u 11-Apr-12 8:50am    
The answer is fine.I also given how to rectify it in different method..But i need to know how the compiler is doing the job for this or what is happening in background..??
Frank R. Haugen 11-Apr-12 19:07pm    
http://msdn.microsoft.com/en-us/library/84zwa5ab(v=vs.110).aspx

I hate it when people answer my question with a link to a reference, but it is sometimes necessary, like now :P
BillW33 11-Apr-12 10:22am    
Also read this article on the Decimal type: http://msdn.microsoft.com/en-us/library/364x0z75.aspx
VJ Reddy 11-Apr-12 19:26pm    
To the down voted member:
Can you please give the reason for down voting, so that I can know what is the deficiency in the solution and correct it.
Sergey Alexandrovich Kryukov 11-Apr-12 22:14pm    
Many votes are apparently irrational. I noticed that my posts with any kind of strong opinions are very likely down-voted. This is probably just a kind of social behavior, an outbreak of personal irritation or something.

However, not everything is clear in this question. The problem is somewhat more complex. Please see other posts and a couple of my comments in reply to Richard's post. But you illustrated the solution for this particular case; I voted 4.
--SA
This is part of the problem of using float or double values, you can and will lose precision when converting between the values and their string representations, since floating point values are only approximations of the values.

When using numbers where accuracy is important you should use integers only. For example when using values representing dollars and cents you should split the string at the decimal point, convert both to integers and thence to total number of cents. When you wish to display any values you need to reconvert to their separate parts.
 
Share this answer
 
Comments
samu4u 11-Apr-12 8:49am    
it is not the problem with conversion of string to double..because it is printing exact value..
d=44.22 //double

doll=44 //int ,now there is no fractional part...

d-doll //(44.22-44) should be .22 but the result is .219999999999986


question is that how the value is loosing... data is exact...
Richard MacCutchan 11-Apr-12 9:07am    
As I stated above, floating point values are approximations; if you want accurate answers then you must use integers. There is no other answer.
Sergey Alexandrovich Kryukov 11-Apr-12 22:02pm    
I did not vote, but... your statement is at least inaccurate. Yes, in this case integers work, but do you know the general case? The nature of real numbers is very different from integer or rational numbers; and when the real numbers are needed, the floating-point representation provides the best approximation. The number of such cases is unlimited. Both mathematical abstractions cannot be perfectly represented in finite state machine like computers: integers are limited in domain ("range" in programming jargon), and floating point values are limited in both domain and accuracy. The problem is wider and more complex than you tried to express. You "must use integers" does not provide a practical advice; and making a recipe on where one could use your approach is a difficult task...
--SA
Richard MacCutchan 12-Apr-12 3:57am    
I stand by my comments, floating point values are inherently inaccurate (as you say) so my statement that you must use integers where accuracy is important still holds. I'm not sure what your argument is, as you seem to be agreeing with my point.
I got a link..
I just started reading...
So i cant able to conclude it...
But i hop after reading this complete doc, we can understand why it is printing this output...

Hope you will read it...

If you want knowledge,read it completely...

What Every Computer Scientist Should Know About Floating-Point Arithmetic[^]
 
Share this answer
 
Comments
Richard MacCutchan 11-Apr-12 9:38am    
This is something you should have learned at the beginning of your studies as it is extremely important. If you write any sort of financial system and use floating point or double variables to hold monetary values, you are heading for serious trouble. Don't do it, ever.
Sergey Alexandrovich Kryukov 11-Apr-12 22:08pm    
Oh... I already had this argument... afraid of coming here again. But... just to uphold the truth. No. There are many cases where you need floating-point values with money. By some reason, you think the money is one single concept. It is not. If you program the bank operational day cycle, all should be in the fixed amount of cents, so you use only integer. But here is a different example: the exchange between different currency. The exchange factor is a real number. And mean values obtained by averaging of some monetary factors... If you still think that money is always an integer value, I don't know how to discuss things any further...
--SA
Richard MacCutchan 12-Apr-12 4:09am    
You are quite right, I was not thinking of this case.
Sergey Alexandrovich Kryukov 12-Apr-12 10:37am    
Thank you for understanding.

I was actually trying to reproduce the know idea when one can use the flaw of the "round to integer amount of cents immediately" method to accumulate some amount of round-off value and make money on it. If there is something like a cost-per-meter of some material, one can adjust the ordered amount of material ordered in small pieces slightly the way the rounding would always go either up it down, to the benefit of the middleman...
--SA
Frank R. Haugen 12-Apr-12 11:42am    
It's a bad answer, but it was an enlighting link. I have never needed that specific data, but now I know for future reference :D
You should have broken out the detail:

C#
string str = "44.22";
double d = Convert.ToDouble(str);
Console.WriteLine("the d val" + d);
double c;
int doll;
int cent;
doll = (int)d;
Console.WriteLine(doll);      //44
c = d - doll; //0.21999999999999886
cent = (int)(c * 100);
Console.WriteLine(cent);     //21

//Try this instead:
int cents = (int)(d * 100);
int dollars = cents / 100;
cents = cents % 100;


What happens is that you are using a truncate when you use the cast.

You can also use the Convert.ToInt32(c *100) because that does not give you the same truncate error as casting.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900