A Razor page has to show two different currencies together, that is: formatting the numbers with periods and commas but without the symbol. Thus, instead of:
R$ 3,000.00 US$ 9,000.00
There would only be:
3,000.00 9,000.00
I'm using the data annotation approach like
[DataType(DataType.Currency)]
public float PrecoUsd { get; set; }
And surely enough the symbol appears. How to not have it?
DataTypeAttribute
with DataType.Currency
enum value sets an input as currency value with symbol mark depending on current culture or globalization attribute being set in web.config. Based from .NET numeric format strings, there are multiple representations you can use with DisplayFormatAttribute
using DataFormatString
:
[DisplayFormat(DataFormatString = "{0:N2}")]
public float PrecoUsd { get; set; }
// using placeholder '#'
[DisplayFormat(DataFormatString = "{0:#,#.00}")]
public float PrecoUsd { get; set; }
// using placeholder '0'
[DisplayFormat(DataFormatString = "{0:0,0.00}")]
public float PrecoUsd { get; set; }
Both of them can use DisplayFor
or EditorFor
to display values:
@Html.DisplayFor(model => model.PrecoUsd)
@Html.EditorFor(model => model.PrecoUsd)
NB: Usage of #,#.##
returns 3,000 instead of desired 3,000.00 (see this demo to prove it). As for displaying currency symbol separated from number input, use DisplayAttribute
e.g. [Display(Name = "US$")]
.