c# - Floating-point error when not assigned to a float variable -


I have a static method 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%; }}   

$ 23.79 for the price, this method gives 78 cents instead of 79. While debugging, I have unknowingly set the bug as follows:

  Public Fixed Class Price Unit {Public Fixed Ent Fret (Float Price) {Float Price = $ 100 value; Int priceInt = (price); Return value value 100%; }}   

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 int bar float float .

Thanks for your reply in advance!

As written in the comments, you have a problem of decimal To avoid the float instead of type in the form.

Using the value 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);   

The output of this code is

 23.79 78  

Another possibility of the problem you experience is that 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.

Before calculating the fraction, you should be able to solve your problem by rounding the value:

  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);   

is now output

 23.7979   

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -