Are you ever aware that if you have a large Number
data type value in JavaScript, the number precession will be lost after a certain point.
I first became aware of this after entering an exceptionally large value number on C# ASP.NET Core. Below is a sample example.
var builder = WebApplication.CreateBuilder(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddCors(s => { s.AddPolicy("AllowAll", s => s.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()); }); var app = builder.Build(); app.Urls.Add("http://*:5001"); app.UseSwagger(); app.UseSwaggerUI(); app.UseCors("AllowAll"); app.MapGet("/", () => "Please go to /swagger for Swagger UI.").WithName("Hello"); var number = 12345678901234567890; app.MapGet("/largenumber", () => new { Number = number, StringMessage = number.ToString() }); app.Run();
Below is the result of the swagger response, as you can see the number “12345678901234567890” will become 12345678901234567000.
Further investigation, you will find it was due to a limitation on JavaScript number. If you try to type in the value in Chrome Browser console, you will already be able to simulate the issue.
Therefore, the lost precision is a limitation on JavaScript instead of ASP.NET Core problem. If refer to the MDN documentation, it does mention clearly it can only safely store integers in the range -(2^53 − 1) (Number.MIN_SAFE_INTEGER) to 2^53 − 1 (Number.MAX_SAFE_INTEGER).
Reference:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type
https://jsoneditoronline.org/indepth/parse/why-does-json-parse-corrupt-large-numbers/
https://softwareengineering.stackexchange.com/questions/353131/best-practice-for-potentially-large-json-numeric-vaues