Need to return the value with an Currency sign

I have a small tab on an app that allows user to lookup a price in a price table and it returns the price. how can i get the '$' sign on the returned price?

public double? SearchMedicinePrice(Int64 pNDC) { double? retValue = null; objConext = new FDBEntities(); Medicine objMedicine = objConext.Medicines.Where(med => med.PriceType == 9 && med.NDC == pNDC).OrderByDescending(item=>item.MedicineID).FirstOrDefault(); if (objMedicine != null) { retValue = objMedicine.Price; } return retValue; }

--------------Solutions-------------

When you call ToString() on a double (and many other types), you can pass in a format string to specify how the result should be formatted. Like this:

double price = 10.5;
price.ToString("C");

Output = $10.50

You could also use String.Format like this:

string message = String.Format("The price is {0:C}", SearchMedicinePrice(pndc));

{0:C} is a format string. The C specifies that the value should be formatted as currency.

You can also pass in a CultureInfo object to configure things like the currency symbol. For example, the following code will output £10.50

10.5.ToString("C", new CultureInfo("en-GB"))

You might also consider using decimal instead of double for storing currency data.

You could build a small object:

public class Currency {
public double Amount {get;set;}
public string Symbol {get;set;}
public override string ToString() {
return String.Format(Symbol + "{0}",Amount);
}

}

public Currency SearchMedicinePrice(Int64 pNDC) {
Currency retValue = null;
objConext = new FDBEntities();
Medicine objMedicine = objConext.Medicines.Where(med => med.PriceType == 9 && med.NDC == pNDC).OrderByDescending(item=>item.MedicineID).FirstOrDefault();
if (objMedicine != null)
{
retValue.Amount = objMedicine.Price;
retValue.Symbol = objMedicine.CurrencySymbol;
}
return retValue;
}

Then in to use it:

Label1.Text = SearchMedicinePrice(pNDC).ToString();

You have to use string

string str="$"+SearchMedicinePrice(x).ToString();

Category:c# Time:2012-03-30 Views:1

Related post

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 2.253 (s). 13 q(s)