Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

WebApi result lost decimal property's precision? #58868

Answered by engineering87
Rorschach331 asked this question in Q&A
Discussion options

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?

You must be logged in to vote

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

Comment options

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.

You must be logged in to vote
1 reply
Comment options

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.

Answer selected by Rorschach331
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /