I am sending via ViewData values to my view like this.
Controller
public List<string> ListarTarjetas()
{
OpexDB db = new OpexDB();
List<string> Tarjetas = new List<string>();
using (db)
{
var listTarjetasInformativas = db.SP_TARJETASINFORMATIVAS();
foreach(var item in listTarjetasInformativas)
{
Tarjetas.Add(item.NOMBRE);
Tarjetas.Add(item.MONTO.ToString());
}
}
return Tarjetas;
}
public ActionResult Index()
{
var DatosTarjetas = ListarTarjetas();
ViewData["PrimerNombre"] = DatosTarjetas[0];
ViewData["PrimerMonto"] = String.Format(DatosTarjetas[1], new CultureInfo("es-HN"));
return View();
}
Part of my razor view.
<div >
<p>@ViewData["PrimerNombre"]</p>
<p>@ViewData["PrimerMonto"]</p>
<div >
<i ></i>
</div>
</div>
Within my view, in the part of <p>@ViewData["PrimerMonto"]</p>
this value 7218.19 is reflected, but I would like it to be reflected in this way, 7,218.19.
I have tried this way,
ViewData["PrimerMonto"] = String.Format(DatosTarjetas[1], new CultureInfo("es-HN"));
and in other ways too. But I don't get any change.
Thank you for any help you can receive.
CodePudding user response:
You can format it just as a number or as currency, but if it's a string in your view data, you need to convert it to a number. See also https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings
[TestMethod]
public void TestNumberFormat()
{
string numberStringValue = "7891.12";
double numberNumericValue = double.Parse(numberStringValue);
string formattedValueWithSymbol = string.Format(new CultureInfo("es-HN"), "{0:c}", numberNumericValue);
string formattedValue = string.Format("{0:N}", numberNumericValue);
Console.WriteLine(formattedValueWithSymbol);
Console.WriteLine(formattedValue);
}
The output of this will be
Standard Output:
L7,891.12
7,891.12