Hi. I want to be able to store specific number of decimals in a model. What I have is
C#
public decimal Cota { get; set; }but how do I allow storage of 112.45 and prevent 112.4543 for example? only two digits after the point
Hi. I want to be able to store specific number of decimals in a model. What I have is
public decimal Cota { get; set; }but how do I allow storage of 112.45 and prevent 112.4543 for example? only two digits after the point
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vishal YelveRank #178 · 13.6K reputationPosted Dec 1, 2022, 5:33 AM EST
Hi Marius,
Try this
[DisplayFormat(DataFormatString = "{0:0.00}")] public decimal Cota { get; set; }Amit MohantyRank #112 · 20K reputationPosted Dec 1, 2022, 5:21 AM EST
Try this
decimal.Round(112.4543M, 2, MidpointRounding.AwayFromZero);Marius VasileRank #785 · 2.9K reputationPosted Dec 1, 2022, 5:26 AM EST
Thank you Amit, but 112.4543 was just an example. I meant any number with decimals but only 2 allowed
Marius VasileRank #785 · 2.9K reputationPosted Dec 1, 2022, 5:40 AM EST
Thank you Vishal, that did the trick