Use of td and tr mvc

3

How can I put the comma to the employee's salary or currency type

this is the view

@foreach (var item in Model)                 {

                <tr>
                    <td>@item.EmpId</td>
                    @*<td>@item.EmpNombre @item.EmpApellido</td>*@
                    <td><p style="white-space: nowrap;">@item.EmpNombre @item.EmpApellido</p></td> 
                    <td>@item.PueDescripcion</td>
                    <td>@item.sueldo</td>
                    <td>@item.Ihss</td>
                    <td>@item.rap</td>



                    @*Deducciones Empleado*@
                    @if (ViewBag.DeduccionesEmpleados != null)
                    {

                        foreach (var Deduccion in (List<string>)ViewBag.Deducciones)
                        {
                            foreach (var Deduccionees in (IEnumerable<RecursosHumanos.Models.Con_DetalleDeduccionesEmpleado>)ViewBag.DeduccionesEmpleados)
                            {

                                if (Deduccionees.EmpId == item.EmpId)
                                {
                                    if (Deduccionees.DedDescripcion == Deduccion)
                                    {
                                        <th>@Deduccionees.DetDedEmpValor</th>

                                        break;


                                    }

                                    else
                                    {
                                        <th> </th>
                                    }
                                }

                            }

                        }

                    }
    
asked by Marco Eufragio 17.10.2018 в 19:44
source

3 answers

6

You can use the String.format method to format it in decimals and add the sign to the desired amount something like this:

<td>
    @String.format("{0:C}",item.sueldo)
</td>

the result varies depending on the configuration of your team.

I leave the Documentation if you need more information about the method.

    
answered by 17.10.2018 / 20:00
source
2

It is a question of format as they put in the accepted answer, but that format can vary according to the regional configuration of the server or Web.config .

Completed a bit:

@{
  var cultureEs = new System.Globalization.CultureInfo("es-ES");
  var cultureEsAr = new System.Globalization.CultureInfo("es-AR");
  var cultureEnGb = new System.Globalization.CultureInfo("en-GB");
}

@foreach(var item in Model) 
{
  <tr>
     // ...
     // suponiendo que sueldo = 2564.456

     <td>@item.sueldo.ToString("C")</td>  <!-- $2,564.46 -->
     <td>@item.sueldo.ToString("C3")</td> <!-- $2,564.456 -->
     <td>@item.sueldo.ToString("C4")</td> <!-- $2,564.4560 -->

     <td>@item.sueldo.ToString("C", cultureEs)</td>   <!-- 2.564,46 € -->
     <td>@item.sueldo.ToString("C", cultureEsAr)</td> <!-- $ 2.564,46 -->
     <td>@item.sueldo.ToString("C", cultureEnGb)</td> <!-- £2,564.46 -->

     // ...
  </td>
}
    
answered by 20.10.2018 в 05:16
1

Replace the. by the, and at the beginning the sign of weights

 function filterFloat(evt,input){
    // Backspace = 8, Enter = 13, ‘0′ = 48, ‘9′ = 57, ‘.’ = 46, ‘-’ = 43
    var key = window.Event ? evt.which : evt.keyCode;    
    var chark = String.fromCharCode(key);
    var tempValue = input.value+chark;
    if(key >= 48 && key <= 57){
        if(filter(tempValue)=== false){
            return false;
        }else{       
            return true;
        }
    }else{
          if(key == 8 || key == 13 || key == 0) {     
              return true;              
          }else if(key == 46){
                if(filter(tempValue)=== false){
                    return false;
                }else{       
                    return true;
                }
          }else{
              return false;
          }
    }
}
function filter(__val__){//VALIDACION PARA MONTO
    var preg = /^([0-9]+\.?[0-9]{0,2})$/; 
    if(preg.test(__val__) === true){
        return true;
    }else{
       return false;
    }   
}

and in the html texbox you put

<input type="text" name="monto" id="monto" onkeypress="return filterFloat(event,this);" value="">
    
answered by 17.10.2018 в 19:49