1

I have a Globalize based project set with the Portuguese language "pt-PT", so the decimal number is separate with the comma ",".

I would prefer not to set the comma in the format of the property if possible.

The code I have now (Model):

    [DisplayFormat(DataFormatString = "{0:#,##}", ApplyFormatInEditMode = true)]
    public decimal SizeOpenedWidth { get; set; }

The View:

    @Html.EditorFor(model => model.SizeOpenedWidth, new { htmlAttributes = new { @class = "form-control" } })

The problem is that this solution has the comma "," harcoded in the Model, and it presents for the decimal "11,01", the value "11".

EDIT1:

I was able to find the problem:

{0:#,##} -> {0:#.##}

I replaced the "," with the "." and it's working now, and it's assuming the globalized portuguese format "11,01".

Is it possible to present "11,1" has "11,10" with this type of string format?

EDIT2:

The solution {0:#.##} creates the problem ",1" that presents ",1".

Patrick
  • 2,995
  • 14
  • 64
  • 125
  • Apart from hard-coding being a problem, the format is simply wrong. In the format string, `,` is **always** the thousand separator and simply specifies where the culture-specific separator will appear. In your case, you asked for no decimal values (no decimal separator in the format string). – Panagiotis Kanavos Jan 20 '16 at 13:00
  • Hi @PanagiotisKanavos thanks but I did not understand your question. The type is decimal and my issue is that the code for now is not presenting the decimal part ",01" for the decimal "11,01" – Patrick Jan 20 '16 at 13:59
  • You need to add the decimal separator in a format string if you want to display any decimals, eg `#,###.##`. You only entered the *thousand* separator. This `1000.01.ToString("#,##")` will return "1.000" in a Protuguese locale and "1,000" in an English locale. You *can't* change the character used for the decimal separator in the format string. To do that you need to change the CurrentCulture of the executing thread, or specify a different culture with a page directive – Panagiotis Kanavos Jan 20 '16 at 14:38
  • Yes, this is why I have now the "." separator in the DataFormatString :) – Patrick Jan 20 '16 at 14:41
  • Also check [this relevant question](http://stackoverflow.com/questions/1560796/set-culture-in-an-asp-net-mvc-app) where a route parameter is used to set the thread's locale – Panagiotis Kanavos Jan 20 '16 at 14:41
  • Local is not an issue because the globalize is working fine for Portuguese decimal format – Patrick Jan 20 '16 at 14:41
  • to show 11,10 when value is 11.1 use `"0.00"` as format specifier. See this [Fiddle](https://dotnetfiddle.net/UgviCm) Also note that using "#.whatever" will not show unit value if it's 0 `decimal x = 0.12M; x.ToString(#.##)` = `",12"` – Gian Paolo Jan 20 '16 at 14:48
  • @GianPaolo thanks, so it seems that I can't achieve what I want to do :( – Patrick Jan 20 '16 at 15:08
  • @Patrick why?, what do you want to achieve? if you want to format 0.1 as `"0,1"`, use as `0.##` (or `0.0#`), if you want `"0.10"` use `0.00` as format specifier. `#` means "show a digit if it's significative" (i.e. remove trailing decimal 0 and leading integer 0), while `0` means "show a digit anyway" – Gian Paolo Jan 20 '16 at 19:51
  • @GianPaolo based on 0.## if I want to show "11,10" when I insert "11,1" ? – Patrick Jan 20 '16 at 22:54
  • to show 11.1 as 11.00 use 0.00, it means "show always 2 and only 2 decimal figures" – Gian Paolo Jan 21 '16 at 21:11

2 Answers2

1

You do not need and should not hardcode the format in the model. I am not sure why don't you simply use the following line in your web.config:

<globalization culture="pt-PT" uiCulture="pt-PT" />

If the problem are 2 decimal places, then this is how the default editor for decimal works in MVC. A simple solution would be to use a TextBoxFor instead of EditorFor (as you actually do not want to use the functionality of the decimal editor).

Anton
  • 2,458
  • 2
  • 18
  • 30
  • Perhaps not even that - ASP.NET considers the browser's locale when deciding what locale to use. Even if there is a requirement to allow users to specify their own locales, this could be done by setting the thread's culture. This could be done using route parameters, eg as shown in [this question](http://stackoverflow.com/questions/1560796/set-culture-in-an-asp-net-mvc-app) – Panagiotis Kanavos Jan 20 '16 at 12:57
  • Hi @Anton thanks for you answer, but with your solution, I still get the format "11,00", and I just want to present "11" when I don't have any decimal part. – Patrick Jan 20 '16 at 14:02
  • Ah, missed the point, sorry - please take a look at the updated answer. – Anton Jan 20 '16 at 14:23
  • Sorry @Anton but I did not understand your update and comment :( – Patrick Jan 20 '16 at 14:40
  • well, I meant that you do use @Html.EditorFor(model => model.SizeOpenedWidth, ...), right? But what is the purpose of using the EditorFor helper if you actually do not want the generated text input to behave the way it is defined by the EditorFor helper? Therefore, instead of using EditorFor you can use @Html.TextBoxFor(...) helper and it will behave the way you want (hopefully). – Anton Jan 20 '16 at 15:20
0

The number formatting will replace the comma with a dot if your culture uses a decimal comma.

From https://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx

Determines the location of the decimal separator in the result string.

More information: The "." Custom Specifier.
0.45678 ("0.00", en-US) -> 0.46
0.45678 ("0.00", fr-FR) -> 0,46

So though the '.' and ',' are used to represent the locales' decimal separator and thousands separator respectively.

Jens Meinecke
  • 2,904
  • 17
  • 20
  • Hi @Übercoder thanks for you answer, but the problem resides in the fact that I get the format "11,00", and I just want to present "11" when I don't have any decimal part. – Patrick Jan 20 '16 at 14:03
  • @Patrick - sorry misunderstood. Surely all you need to do in this case is to change the format specification to this: `[DisplayFormat(DataFormatString = "{0:#}", ApplyFormatInEditMode = true)]` – Jens Meinecke Jan 20 '16 at 21:35
  • I have tried but for "11,01", when I edit the value, I get "11" – Patrick Jan 20 '16 at 22:50
  • @Patrick so let's get this straight - for numbers that are whole you do not want the `,00` printed, but for values that do have a decimal fraction, you **do** want that fraction printed? If so just remove the `:#` from the format specifier. Let the CLR do the default formatting - which appears to be what you want – Jens Meinecke Jan 20 '16 at 23:02
  • If I remove :# and only set {0}, I get "10,00" and I only want "10" – Patrick Jan 20 '16 at 23:05