padavan
padavan

Reputation: 884

.net 6 web api request change float model

I send request from web: enter image description here

in model:

public float Team { get; set; }

But i got incorrect value:

enter image description here

What is problem with rounded ? How fix it in webapi?

I have many method , fields , levels i dont want resolve by hand every fields by Math.Round or other functions ....

update 1. for @MdFaridUddinKiron

public IActionResult Save([FromBody] Model req){
     return Ok();
}

public class Model {
   public float Team { get; set; }
}

Upvotes: 0

Views: 738

Answers (1)

Jiulia
Jiulia

Reputation: 323

try using a double instead.

floats, by how they are rapresented, are not good for precision unless we are talking about extremely small numbers.

Here is a nice little article explaining why floats may lose precision (its for cpp, but the general concept should be the same for c#): https://learn.microsoft.com/en-us/cpp/build/why-floating-point-numbers-may-lose-precision?view=msvc-170

Edit: I found this 13 years old question in the suggested section that might definitivelty help you understand more about floats and doubles: Difference between decimal, float and double in .NET?

Upvotes: 1

Related Questions