Save 3 decimal places to sqlServer

Asked

Viewed 133 times

1

Personal I have to save 3 decimal places in my database SQLSERVER, however this saving only 2 houses for example I have a value 0.456 and when saving is 0.450 in the code :

using System.ComponentModel.DataAnnotations;

    public class TB_ItensPedidoCompra
    {
    public decimal ValorUnitario{ get; set; }

    [MaxLength(10)]
    public string TipoSelecionado { get; set; }

 }

In my bank layer it’s like this:

 bdProduto.ValorUnitario = Convert.ToDecimal(vlUnitario);

the value up to vlUnitario enough 0.456, but when saved is 0.450

Could someone help me

in my bank edited

Nome da coluna = ValorUnitario  Tipo de Dados = decimal(18, 4)
  • How is persistence being made in the database? Insert, Procedure... is using what in the data layer? ADO, EF, NHIBERNATE, etc...

  • I am using EF and saved context.Savechanges();

  • If I changed the guy to Double it wouldn’t work?

  • Apparently the 2 digit accuracy is the standard in EF. See link https://storiknow.com/entity-framework-decimal-scale-and-precision-convention/ which presents a solution to the same situation you are facing. Basically, create a new class of the attribute type, add it in the construction of Dbcontext in OnModelCreating and decorate the property (Valorunitario) of your object.

  • 3

    @Natanbarros and why he would do something so wrong?

  • It didn’t work I’m not managing to decorate the property (Valorunitario)

  • @Maniero It was just a question, if you’re wrong, explain why... I didn’t think your criticism was bad, but if you’re going to criticize a person who probably knows less than you explain... I said this because when I stopped using decimal and used double in my program it worked...

  • I also asked only one question. https://answall.com/q/219211/101

  • A question saying it is something wrong (in question format, which was ironic)...I even posted in the comments to see someone correct me if it was wrong, but I expected arguments, thank you for your reply.

  • 1

    @Natanbarros I changed my variable to float and solved the problem, I thought I had a way to use the decimal most really have no thanks for the help

  • @Denilsonana For nothing...

Show 6 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.