c# - Floating-point error when not assigned to a float variable -
I have a static method $ 23.79 for the price, this method gives 78 cents instead of 79. While debugging, I have unknowingly set the bug as follows: I do not understand that assigning (100 * value) in the float variable solves the problem. The FAIX, the whole number of casting is done on the same value as Thanks for your reply in advance! As written in the comments, you have a problem of Using the value The output of this code is Another possibility of the problem you experience is that Before calculating the fraction, you should be able to solve your problem by rounding the value: is now output GetFraction which gives the st in the price below:
Public static class PriceUtil {public static intra fracturing (float price) {int price int = (int *) (100 * value); Return value value 100%; }}
Public Fixed Class Price Unit {Public Fixed Ent Fret (Float Price) {Float Price = $ 100 value; Int priceInt = (price); Return value value 100%; }}
int bar
float
float .
decimal To avoid the
float instead of type in the form.
23.79f does not describe the problem My computer is therefore my guess that the price has been calculated from the last few calculations. Here's a demonstration:
var value = 23.7899999f; Console.WriteLine (value); Var fraction = ((int * (value * 100))% 100; Console.WriteLine (fraction);
23.79 78
float is a 32-bit floating point number and 80 bit floating point numbers are used internally on CPU counting. If you change the way you complete your calculations, you may find a slightly different depending on rounding or truncation. Results can be found. However, I suspect that this is an explanation of the results of your experience.
Var value = 23.789999 9F; Console.WriteLine (value); var round value = math Round (value, 2); Var fraction = ((int) 100 * round value)% 100; Console.WriteLine (fraction);
23.7979
Comments
Post a Comment