0

I would like to parse the decimal 4.4589 to 4.45. But, I couldnt have succeeded. I have tried to use

parseFloat(yourString).toFixed(2)

but it didnt work...

Please help me to parse the "BanknoteBuying" and "BanknoteSelling".

Thanks in advance

[HttpGet]
public ActionResult GetData()
{
    XDocument doc = XDocument.Load("http://www.tcmb.gov.tr/kurlar/today.xml");
    var dox = doc.Descendants()
        .Where(r => r.Name == "Currency")
        .Select(r => new {
            Isim = r.Element("Isim").Value,
            Kod = r.Attribute("Kod").Value,
            AlisKur = r.Element("BanknoteBuying").Value,
            SatisKur = r.Element("BanknoteSelling").Value
        }).Where(x=>x.Isim== "ABD DOLARI"||x.Isim== "EURO");

    return Json(dox, JsonRequestBehavior.AllowGet);
}
Arif YILMAZ
  • 5,754
  • 26
  • 104
  • 189
  • 1
    `parseFloat(yourString).toFixed(2)` is js, your sample code is c#, IMO string formatting shouldn't be done in your linq query, do it in your angular app – mxmissile May 10 '18 at 13:16
  • if you need decimal point handling in angular check https://stackoverflow.com/a/35514325/2753555 – Nikhil K S May 10 '18 at 13:16
  • 1
    The incoming XML data is in a format that has nothing to do with your users UI culture. So I would parse the AlisKur and StatisKur to `decimal`s on the C# side. Let the JSon tranfer it to `Number` on your client side and worry about the formatting in the actual HTML. – H H May 10 '18 at 13:21

1 Answers1

1

var yourString = "4.4589";

var result = Math.trunc(parseFloat(yourString) * 100) / 100;

console.log(result);
Dillon
  • 687
  • 6
  • 9