-
Notifications
You must be signed in to change notification settings - Fork 10.4k
WebApi result lost decimal property's precision? #58868
-
use the simple web api template:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.MapGet("/weatherforecast", () =>
{
var forecast = new WeatherForecast
(
19493.7797202797203M
);
return forecast;
})
.WithName("GetWeatherForecast")
.WithOpenApi();
app.Run();
record WeatherForecast(decimal Amount)
{
}
when i call the api in the swagger,i get a json like this:
{
"amount": 19493.77972027972
}
the amount value is 19493.77972027972
,but i want the value 19493.7797202797203
,what should i do?
Beta Was this translation helpful? Give feedback.
All reactions
Hello @Rorschach331,
to preserve the full precision, you can work around return it as a string.
If you need it to remain a number, you'll have to write a custom JsonConverter for decimal to control the formatting:
public class DecimalJsonConverter : JsonConverter<decimal> { public override decimal Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => reader.GetDecimal(); public override void Write(Utf8JsonWriter writer, decimal value, JsonSerializerOptions options) { writer.WriteRawValue(value.ToString("G29", System.Globalization.CultureInfo.InvariantCulture)); } }
In your Program.cs:
builder.Services.Configure<Microsoft.Asp...
Replies: 1 comment 1 reply
-
Hello @Rorschach331,
to preserve the full precision, you can work around return it as a string.
If you need it to remain a number, you'll have to write a custom JsonConverter for decimal to control the formatting:
public class DecimalJsonConverter : JsonConverter<decimal> { public override decimal Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => reader.GetDecimal(); public override void Write(Utf8JsonWriter writer, decimal value, JsonSerializerOptions options) { writer.WriteRawValue(value.ToString("G29", System.Globalization.CultureInfo.InvariantCulture)); } }
In your Program.cs:
builder.Services.Configure<Microsoft.AspNetCore.Http.Json.JsonOptions>(options => { options.SerializerOptions.Converters.Add(new DecimalJsonConverter()); });
and use:
record WeatherForecast(decimal Amount); app.MapGet("/weatherforecast", () => { var forecast = new WeatherForecast(19493.7797202797203M); return forecast; });
Try and let me know.
Beta Was this translation helpful? Give feedback.
All reactions
-
Thank you for your reply.
My solution at the time was to return a string to the front-end.
I tested the DecimalJsonConverter
and it worked fine.
However, I think this issue is caused by JavaScript Number type. To correctly handle high-precision numbers, the front-end needs to reference a library like decimal.js.
Beta Was this translation helpful? Give feedback.