Mask in table values

0

I am looking for a way to create "masks" for the values of my table in ASP.NET. Currently, I'm displaying values like this:

@model TB_RESUMO_GERAL
List<TB_RESUMO_GERAL> rg = (List<TB_RESUMO_GERAL>)ViewData["relatorioGeral"];
    .
    .
    .
<table class="table table-striped table-bordered">
        <tr>
            <th>Mês</th>
            <th>Val. Esperado</th>
            <th>Val. Adquirido</th>
            <th>Recebimento Mês</th>
            <th>Atraso</th>
            <th>Amortização</th>
            <th>Selic</th>
            <th>Premio selic</th>
            <th>Taxa Gestão</th>
            <th>Seguro</th>
            <th>Despesas</th>
            <th>Não identificados</th>
            <th>Valor Disponível</th>
            <th>Contratos Ativos</th>
        </tr>
        @foreach (TB_RESUMO_GERAL resumo in rg)
        {
            <tr>
                <td>@resumo.DT_MES_UTILIZACAO</td>
                <td><span class="money" /> @resumo.VL_ESPERADO</td>
                <td><span class="money" /> @resumo.VL_DISPONIVEL</td>
                <td><span class="money" /> @resumo.VL_RECEBIMENTO_MES</td>
                <td><span class="money" /> @resumo.VL_ATRASO</td>
                <td><span class="money" /> @resumo.VL_AMORTIZACAO</td>
                <td><span class="money" /> @resumo.VL_SELIC</td>
                <td><span class="money" /> @resumo.VL_PREMIO_SELIC</td>
                <td><span class="money" /> @resumo.VL_TAXA_GESTAO</td>
                <td><span class="money" /> @resumo.VL_SEGURO</td>
                <td><span class="money" /> @resumo.VL_DESPESAS</td>
                <td><span class="money" /> @resumo.VL_NAO_IDENTIFICADO</td>
                <td><span class="money" /> @resumo.VL_DISPONIVEL</td>
                <td><span class="money" /> @resumo.QTD_CONTRATOS_ATIVOS</td>
            </tr>
        }
    </table>

And the values are coming out like this:

I would like to put thousands and thousands of points for pennies, as well as take that timetable out of date. Ex (R $ 1,245.50) (02/2016).

    
asked by anonymous 03.08.2017 / 16:13

3 answers

4

As you are using C #, what I would use in your case to not need to use another JS library would be:

For money values, it would double and apply the currency of String.Format("{0:C}",100) //sendo 100 = valor que você transformou em Double .

For date simply convert to string by passing the format you want, remembering that it needs to be DateTime , type Date.ToString("dd/MM/yyyy"); See the example at the link below:

Example

Remembering that the Currency currency is related to the culture of your server or session. In the example is $ USD because the server is American, I believe that in your PC will be R $

    
03.08.2017 / 17:02
3

Well I'm using this plugin link where you open the file src = > jquery.mask.js and it imports for your project and makes the appropriate reference.

in your view:

<script type="text/javascript" src="~/Scripts/jquery.mask.js"></script >

     @model TB_RESUMO_GERAL
    List<TB_RESUMO_GERAL> rg = (List<TB_RESUMO_GERAL>)ViewData["relatorioGeral"];
        .
        .
        .
    <table class="table table-striped table-bordered">
            <tr>
                <th>Mês</th>
                <th>Val. Esperado</th>
                <th>Val. Adquirido</th>
                <th>Recebimento Mês</th>
                <th>Atraso</th>
                <th>Amortização</th>
                <th>Selic</th>
                <th>Premio selic</th>
                <th>Taxa Gestão</th>
                <th>Seguro</th>
                <th>Despesas</th>
                <th>Não identificados</th>
                <th>Valor Disponível</th>
                <th>Contratos Ativos</th>
            </tr>
            @foreach (TB_RESUMO_GERAL resumo in rg)
            {
                <tr>
                    <td><span class ="date">@resumo.DT_MES_UTILIZACAO</td>
                    <td><span class="money" /> @resumo.VL_ESPERADO</td>
                    <td><span class="money" /> @resumo.VL_DISPONIVEL</td>
                    <td><span class="money" /> @resumo.VL_RECEBIMENTO_MES</td>
                    <td><span class="money" /> @resumo.VL_ATRASO</td>
                    <td><span class="money" /> @resumo.VL_AMORTIZACAO</td>
                    <td><span class="money" /> @resumo.VL_SELIC</td>
                    <td><span class="money" /> @resumo.VL_PREMIO_SELIC</td>
                    <td><span class="money" /> @resumo.VL_TAXA_GESTAO</td>
                    <td><span class="money" /> @resumo.VL_SEGURO</td>
                    <td><span class="money" /> @resumo.VL_DESPESAS</td>
                    <td><span class="money" /> @resumo.VL_NAO_IDENTIFICADO</td>
                    <td><span class="money" /> @resumo.VL_DISPONIVEL</td>
                    <td><span class="money" /> @resumo.QTD_CONTRATOS_ATIVOS</td>
                </tr>
            }
        </table>

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->


$(document).ready(function () {
   $(".data").mask("99/99/9999");
   $(".money").mask("R$###.###.###,##");
});
    
03.08.2017 / 16:58
1

Thank you for your help! I had trouble using String.Format("{0:C}", ValorDinheiro) , so I made a method to mount my mask. I hope it helps others:

public static string FormataDinheiro(string dinheiro) {
        string retorno = ""; int diferença = 2; int length = dinheiro.Length;

        if(length > 2){
            if(length < 6){
                retorno = dinheiro.Substring(0, length - diferença) + "," + dinheiro.Substring(length - diferença, 2);
                diferença = length;
            }else{
                retorno = "," + dinheiro.Substring(length - diferença, 2);
            }
        }
        while((length - diferença) > 0){
            if(diferença + 3 < length){
                diferença += 3;
                retorno = "." + dinheiro.Substring(length - diferença, 3) + retorno;
            }
            else{
                retorno = dinheiro.Substring(0, length - diferença) + retorno;
                diferença = length;
            }
        }
        return retorno;
    }

As for the date, I used String DataComMascara = Date.ToString("dd/MM/yyyy") as per Leonardo Bonetti's tip and it worked perfectly!

    
04.08.2017 / 15:17