asp.net mvc - How to format numbers in C# MVC 4 to reflect a two decimal point currency? -
so i'm having trouble view wanting more numbers behind decimal point input want need.
i using model:
public class movie { public int id { get; set; } public string title { get; set; } public datetime releasedate { get; set; } public string genre { get; set; } [displayformat(dataformatstring = "{0:f2}", applyformatineditmode = true)] public decimal price { get; set; } }
this in view:
<div class="editor-label"> @html.labelfor(model => model.price) </div> <div class="editor-field"> @html.editorfor(model => model.price) @html.validationmessagefor(model => model.price) </div>
it seem adding displayformat attribute movie.price isn't enough allow users enter numbers 2,79, if try they'll validation error. input field created @html.editorfor(model => model.price) instead requires number 2,790 (notice additional 0 added behind decimal).
i've tried variety of dataformatstrings no effect, missing/not understanding?
how adjust code users able enter 2,79 valid input instead of 2,790?
the problem here server - , client-side validation scripts - parsing comma (,
) not decimal separator, thousands separator. because, in default culture, decimal point .
. 1234 + 56/100 "normally" represented 1,234.56
, not 1.234,56
.
you need set proper culture parsing works correctly in format. either globally setting value in web.config, or manually setting thread.currentthread.currentuiculture
/ thread.currentthread.currentculture
controller if need more fine-grained control.
this not bubble down client-side validation, need make sure write proper javascript validation rules override default ones.
as side note, may want indicate users format expect. people other parts of world write numbers differently, , might expect things in native format. (sometimes language of page hint of expected number/date formats though.)
Comments
Post a Comment