Decimal field displays a . 0 on the screen

Asked

Viewed 132 times

0

I have a CPF field that is decimal(11). Well, when I show on the grid the CPF it looks like this: 12345678911.0, as I can remove the . 0?

My Viewmodel

public class FuncionarioViewModel
    {
        [Key]
        public int id { get; set; }

        [Required(ErrorMessage = "Nome do funcionário é obrigatório", AllowEmptyStrings = false)]
        [Display(Name = "Nome")]
        public String nome { get; set; }

        [Required(ErrorMessage = "Data de Nascimento do funcionário é obrigatório", AllowEmptyStrings = false)]
        [Display(Name = "Data de Nascimento")]
        [DataType(DataType.Date, ErrorMessage = "formato de data invalido")]
        public DateTime dataNascimento { get; set; }

        //[Required(ErrorMessage = "CPF do funcionário é obrigatório", AllowEmptyStrings = false)]
        [Display(Name = "CPF")]
        [DataType(DataType.Text, ErrorMessage = "Formato inválido")]
        public String cpf { get; set; }

        [Display(Name = "Nome da Cidade")]
        public String NomeCidade { get; set; }

        [Required(ErrorMessage = "Cidade do funcionário é obrigatório", AllowEmptyStrings = false)]
        [Display(Name = "Cidade")]
        public virtual int cidade { get; set; }
    }

My screen of getemployees

@model IEnumerable<TreinamentoCrud.FuncViewModel.FuncionarioViewModel>

@{
    ViewBag.Title = "IndexVM";
}

<h2>Lista de Funcionários</h2>

<p>
    @Html.ActionLink("Novo Funcionário", "CreateVM")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.nome)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.dataNascimento)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.cpf)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NomeCidade)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.cidade)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.nome)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.dataNascimento)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.cpf)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NomeCidade)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.cidade)
        </td>
        <td>
            @Html.ActionLink("EditVM", "EditVM", new { id=item.id }) |
            @*@Html.ActionLink("Details", "Details", new { id=item.id }) |*@
            @Html.ActionLink("DeleteVM", "DeleteVM", new { id=item.id })
        </td>
    </tr>
}

</table>

Method to get the list of employees

public async Task<List<FuncionarioViewModel>> GetFuncionariosVM()
        {
            string url = $"http://localhost:56137/api/GetFuncionario";
            var response = await client.GetStringAsync(url);
            var _funcionario = JsonConvert.DeserializeObject<List<FuncionarioViewModel>>(response);   


            return _funcionario;
        }
  • Ever tried to convert to integer? And why are you dealing with Cpf as decimal? Usually you use string or integer to represent it.

  • The requirement said number 11, but I think 11 was just the maximum. I think it was really a prank

  • 5

    @pnet do not know if I asked this before, have you ever thought of taking a course to understand how things work?

  • 1

    @pnet CPF is a standard document used in Brazil it will always have 11 digits and is made in a way in which the first 9 digits are the number itself and the last 2 are check digits, so it is usually treated as text, because there is a formula to check whether Cpf is valid or not. It is usually specified as a numeric field because Cpf only has numbers, but at the programming and database level it can be text.

  • @Guilhermebatista, I fully agree with you, but if I switch on my own, I get in trouble. The requirement says, number 11 and the only numerical field I know that accepts length is decimal. I spoke to a colleague and he told me to do as required.

  • 2

    @pnet I warn you that you will have problems with this method. Numeric types such as int, long, float, etc., ignore the 0(zero) on the left and this information will be lost in both the bank and its code. So all cpfs starting with zero will not work. And yes, there is Cpf starting with 0.

  • So in this case I use a padleft(11) to show on the grid and in the bank as well

Show 2 more comments

1 answer

3

You need to Convert to long. Because to convert to whole it can exceed the limit.

Maximum value for a type int variable. 2147483647

decimal cpf = 79399965412; // vem do banco tipo decimal
long novoCpf = (long)cpf ; // transforma pra long

Another option is to work with it in Varchar format in the bank.

Browser other questions tagged

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