0

I'm new in dotNet development and trying to create an API with response having decimals. I'm expecting that the API will show 0.000000 but instead only shows 0.

This is the data from my database

database screenshot

public class InvAmt
    {
        public double buyWsAmount { get; set; }
        public double buyRetAmount { get; set; }
        public double sellWsAmount { get; set; }
        public double sellRetAmount { get; set; }
    }
[HttpGet]
public async Task<ActionResult<List<InvAmt>>> GetInvAmt()
 {
   var connString = _config.GetConnectionString("Mysqlconn");

   using (IDbConnection conn = new MySqlConnection(connString))
    {
      string sql = $@"SELECT pa.buy_ws_amount AS buyWsAmount, pa.buy_retail_amount AS buyRetAmount, pa.sell_ws_amount AS sellWsAmount, pa.sell_retail_amount AS sellRetAmount FROM product_amount pa;";

      var res = await conn.QueryAsync<ProductCategory>(sql, null);
      return res.ToList();
     }
  }

API response

[
  {
    "id": 1
    "buyWsAmount": 1300,
    "buyRetAmount": 26,
    "sellWsAmount": 1800,
    "sellRetAmount": 36.000001,
  },
  {
    "id": 52
    "buyWsAmount": 0,
    "buyRetAmount": 0,
    "sellWsAmount": 0,
    "sellRetAmount": 0,
  }
]
Bagus Tesa
  • 1,317
  • 2
  • 19
  • 42
Ino
  • 1
  • 3
  • 2
    AFAIK this isn't supported natively with System.Text.Json. You'll have to write a custom converter to handle this. [This answer](https://stackoverflow.com/a/34569851/2084984) sums it up nicely. – jhmckimm Aug 06 '23 at 11:18
  • 2
    It's worth noting, though, that this is purely cosmetic. `0` is equal to `0.00` in JSON, `0` is just a simpler representation. – jhmckimm Aug 06 '23 at 11:23

0 Answers0