2

In my model I want to format the output of a double. I'm trying to output a number like this:

100000000 as 100.000.000 (simply a dot separator)

But by using this (currency format)

[DisplayFormat(DataFormatString = "{0:C}")]

Result => 424.232.344 kr

I also get the currency symbol (depending on the culture, in my case "kr") and I don't want that. I simply want the dot separator but without the currency.

Any suggestions?

ChrisRun
  • 993
  • 1
  • 10
  • 24
  • 1
    Does `{0:#,#}` work? -- See also [Custom Numeric Format Strings](https://msdn.microsoft.com/library/0c899ak8.aspx) – Corak Jan 29 '16 at 08:45
  • check this for more options https://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx – Ian Jan 29 '16 at 08:46
  • @Corak, have tried. It results in spaces instead of dots. – ChrisRun Jan 29 '16 at 08:52
  • @Ian Have checked it, does not contain my desired output. – ChrisRun Jan 29 '16 at 08:52
  • 1
    @Ian - yes, that is probably, because the thousands-separator of your current(UI)culture has it defined like that. Sorry, I'm not versed enough in MVC to know how you can specify a different language. Usually, you can do something like `1000.ToString("#,#", CultureInfo.GetCultureInfo("de-DE"))`. But I think that's not possible with data annotations, because the culture isn't a compile constant. – Corak Jan 29 '16 at 08:58

1 Answers1

5

Because "C" means currency in that format specifier and it uses CurrencySymbol of your CurrentCulture setting.

You can use The "N" format specifier instead.

[DisplayFormat(DataFormatString = "{0:N0}")]

Based on your comment, looks like your CurrentCulture using white space as NumberGroupSeparator but uses . as a CurrencyGroupSeparator.

In such a case, you can set it's NumberGroupSeparator to . and you will be fine.

Read: Is there a way of setting culture for a whole application? All current threads and new threads?

Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • Yes I am aware that C stands for currency. I Have tried your suggestion and it returns 424 232 344,00 (spaces instead of dots.) – ChrisRun Jan 29 '16 at 08:53
  • @ChrisRun Updated my answer. Take a look. – Soner Gönül Jan 29 '16 at 08:57
  • Thanks, but unfortunately I'm not able to set the NumberGroupSeparator in this case, since the instance is readonly. – ChrisRun Jan 29 '16 at 09:45
  • @ChrisRun `NumberGroupSeparator` property is not read-only but your culture setting _might_ be. Have you ever check this question for this? http://stackoverflow.com/q/18728505/447156 – Soner Gönül Jan 29 '16 at 09:50
  • My mistake, yes you are correct, the culture setting is readonly. Have not, will check it out, thanks. – ChrisRun Jan 29 '16 at 09:57