I have this decimal set up:
[Required(ErrorMessage = "Please tell us your income")]
[Display(Name = "How much do you earn from all the income types you selected above?")]
[DataType(DataType.Currency)]
[DisplayFormat(DataFormatString = "{0:c}")]
public decimal IndivdualIncome { get; set; }
I am using [Serializable] on my Model. Using a ViewModel to bring this in to my view. Also using a multi-step wizard.
When I get to a step with this type of input (for capturing decimal values), the text box is empty and I can enter “1000”. This is expected behavior. However, say I click “Next” on the wizard and the next step has a similar text box (again, it’s blank which is expected – call it “SpouseIncome”), but then if I do not enter any value and:
- I hit “Back”, then the “IndividualIncome” textbox will display the value as “1000.00”; and
- If I then hit “Next”, the “SpouseIncome” (which I left blank) now shows “0.00”.
Is it my use of decimal, or possibly the attributes I am using?
Any help on how I can just have it display as either blank (no “0.00”) or to take out the “.00” if a value was input previously is much appreciated.
You probably want to use
"{0:N}"which is number format or"{0:g}", which is general number format instead currency format"{0:c}"you currently using.Or you might want to consider custom number formatting to be very specific on what you looking for.
Something like this
{0:0,0}, which is thousand separator, gives you 1,500.More about string formats and custom number formatting here.
Hope this helps.