0

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; }
}
padavan
  • 714
  • 8
  • 22
  • @Maytham i know about it. Question how its fix in asp.net web api. In NodeJs we havent it problem – padavan Apr 07 '22 at 10:45
  • in asp.net is there no single approach to the web api solution? I wrote in question "have many method , fields , levels i dont want resolve by hand EVERY fields by Math.Round or other functions" – padavan Apr 07 '22 at 10:55
  • Did you able to resolved the issue? – Md Farid Uddin Kiron Apr 08 '22 at 08:48
  • @MdFaridUddinKiron no :( waiting for experts – padavan Apr 08 '22 at 12:29
  • Could you please share your full controller along with the model. – Md Farid Uddin Kiron Apr 08 '22 at 12:41
  • @MdFaridUddinKiron added – padavan Apr 09 '22 at 11:26
  • How are you sending the request? I have just reproduced your issue and I am getting `0.91` as expected. No issue I have founded. Would you kindly check your code? How the the value being sent from your `client side app` I think problem is there not in API. In cases where you are using *.js* kind of language, double check that the value is being passed as expected, since *javascript* often adds extra precision. – Md Farid Uddin Kiron Apr 13 '22 at 08:36

1 Answers1

1

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?

Jiulia
  • 305
  • 2
  • 10