|
-
April 10th, 2006, 06:19 AM
#1
Surprise! 0.77 * 100 == 76
Try the code:
float a = 0.77F;
int b = (int)(100 * a);
Console.WriteLine(b.ToString());
Result is 76.
What do you think about such strange .Net calculate execution ?
-
April 10th, 2006, 06:46 AM
#2
Re: Surprise! 0.77 * 100 == 76
Useful? Then click on (Rate This Post) at the top of this post.
-
April 10th, 2006, 11:40 AM
#3
Re: Surprise! 0.77 * 100 == 76
simple solution 
Code:
decimal a = 0.77M;
int b = (int)(100 * a);
Console.WriteLine(b.ToString());
-
April 10th, 2006, 02:41 PM
#4
Re: Surprise! 0.77 * 100 == 76
can also do it with float
Code:
float a = 0.77f;
int b = Convert.ToInt32(100 * a);
console.WriteLine(b.ToString());
-
April 10th, 2006, 02:56 PM
#5
Re: Surprise! 0.77 * 100 == 76
 Originally Posted by Syrix
simple solution
Code:
decimal a = 0.77M;
int b = (int)(100 * a);
Console.WriteLine(b.ToString());
Solution is not the problem.
I used Round, for example.
When I met the problem I was suppressed by so non-clear behavior of the code.
I always used double before.
Float was returned as object from DataRow element from correspond DB Field:-)
And I do not understand, why 100*0.77f == 76.99999…
Is it Microsoft or Intel?
-
April 10th, 2006, 09:09 PM
#6
Re: Surprise! 0.77 * 100 == 76
 Originally Posted by Alex Rest
And I do not understand, why 100*0.77f == 76.99999…
Is it Microsoft or Intel?
it is because of the processor can never represent exactly 0.77 in binary.
-
April 11th, 2006, 01:35 AM
#7
Re: Surprise! 0.77 * 100 == 76
 Originally Posted by Alex Rest
And I do not understand, why 100*0.77f == 76.99999…
That's why I posted the FAQ...
Useful? Then click on (Rate This Post) at the top of this post.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|