Arturo Martinez Arturo Martinez - 1 month ago 17
C# Question

Convert String to Currency

Im trying to detect if my string has a lenght > 4 convert it to my local currency otherwise make it display no decimal's something like this:

This is my code:

var listDay = data.Where(c => c.Fecha >= actualDate && c.Fecha <= actualDateMax).ToList();
var haveDataDay = listDay.Count() > 0;

<h4 class="semi-bold">
@if (haveDataDay)
{ @valueDateDay.ToString("F2") }
else
{ @this.FP("lbl.loader.nodata") }
</h4>


But i dont know how to check this

Someone has any idea?

Answer

There's probably a more elegant way to do this but this works just fine:

var valueDateDay = 1000;
Console.WriteLine(valueDateDay.ToString(valueDateDay.ToString().Length > 4 ? "N0" : "C2"));
valueDateDay = 100000;
Console.WriteLine(valueDateDay.ToString(valueDateDay.ToString().Length > 4 ? "N0" : "C2"));

See it in action

Comments