I get data from a csv file and parse it in my application. In my csv file, I have a price column, the value of which I use, of course, in the price of an item in my project.
However, the price in the csv file does not contain a trailing 0, for example, if the price and element are $5.00 , then the csv file has it as $5 , if the price is $9.60 , csv has it as $9.6 . Other prices, such as $9.56 , are good, though.
This is how I get the price from the csv file:
Price = string.IsNullOrEmpty(columns[3].Trim()) ? null : (decimal?)decimal.Parse(columns[3]),
public decimal? Price { get; set; } my class price set to public decimal? Price { get; set; } public decimal? Price { get; set; } public decimal? Price { get; set; } .
How do I format what is returned to fix this problem?
Price = String.Format("Price: {0:C}", string.IsNullOrEmpty(columns[3].Trim()) ? null : (decimal?)decimal.Parse(columns[3]));
I tried this but did not work.
How to fix this so that csv values ββlike $5.9 formatted at $5.90 .
EDIT:
I tried:
Price=decimal.Round(string.IsNullOrEmpty(columns[3].Trim()) ? null : (decimal?)decimal.Parse(columns[3]), 2, MidpointRounding.AwayFromZero);
Not sure if I did it right?
Also, I'm not sure how I can use the option below in my code:
decimalVar.ToString ("#.##");
Also tried:
Price = string.IsNullOrEmpty(columns[3].Trim()) ? null : (decimal?)decimal.Parse(columns[3], NumberStyles.Currency)
But still does not work.